我正在使用Google PHP客户端访问电子表格数据。Google Api Php客户端 - Spreadsheets权限错误
我得到这个致命的错误:
Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "code": 403, "message": "The caller does not have permission", "errors": [ { "message": "The caller does not have permission", "domain": "global", "reason": "forbidden" } ], "status": "PERMISSION_DENIED" } }
我的代码:
$client = new Google_Client();
$client->setApplicationName("Google spreadsheets");
$client->setDeveloperKey("xxxxx");
$client->setScopes(array('https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/spreadsheets.readonly',
'https://www.googleapis.com/auth/drive.file'));
$service = new Google_Service_Sheets($client);
$range = 'Class Data!A2:E';
$response = $service->spreadsheets_values->get($sheetid, $range);
$values = $response->getValues();
if (count($values) == 0) {
print "No data found.\n";
} else {
print "Name, Major:\n";
foreach ($values as $row) {
// Print columns A and E, which correspond to indices 0 and 4.
printf("%s, %s\n", $row[0], $row[4]);
}
}
如何解决这一问题?
该电子表格归我所有,意味着可以访问工作表的api项目所有者。此外,我想访问没有oAuth认证。 –
Google Developer Console上的项目不允许您访问使用数据,即使是拥有它的开发人员也是如此。您仍然需要访问私人用户数据的权限。您应该考虑使用服务帐户。您可以预先授予允许其访问表单的服务帐户。查看https://developers.google.com/api-client-library/php/auth/service-accounts – DaImTo
我创建了一个服务帐户并使用下载的json文件。我不确定我们在控制台中授权服务帐户的位置。在链接中,它谈到了gsuite授权。你能指导吗? –