未找到google工作表的服务帐户

问题描述 投票:0回答:1

我正在尝试使用服务帐户阅读电子表格(我无法使用OAuth,因为该过程将在服务器上运行以定​​期检查工作表数据)

我尝试了几种方法。如果我使用oauth跟踪示例,我可以看到工作表值。但是,我需要在后台运行没有任何GUI的脚本。

我找到了这个教程https://github.com/juampynr/google-spreadsheet-reader我创建了一个项目,服务帐户,添加了查看器角色,与服务帐户电子邮件共享电子表格。生成密钥。似乎测试程序可以连接到谷歌服务,但它请求电子表格的那一刻最终结果是“404 not found”。

require 'vendor/autoload.php';

$service_account_file = '/secrets/readsheetmar2019-08b737d1c1cb._portfolio_test.json';


$spreadsheet_id = '1TAWybckPrnWlQxBZh0ScDsFOvftwi2dvTBNGarSdY30';

$spreadsheet_range = '';

putenv('GOOGLE_APPLICATION_CREDENTIALS=' . $service_account_file);
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Sheets::SPREADSHEETS_READONLY);
$client->fetchAccessTokenWithAssertion();
$service = new Google_Service_Sheets($client);

//added by me
if ($client->isAccessTokenExpired()) {
  print "expired\n";
}else{
  print "not expired\n";
}

$result = $service->spreadsheets_values->get($spreadsheet_id, $spreadsheet_range);
var_dump($result->getValues());

错误:PHP致命错误:未捕获异常'Google_Service_Exception',消息'错误404(未找到)!! 1

service google-sheets google-sheets-api google-oauth2 service-accounts
1个回答
1
投票
  • 使用OAuth2检索的访问令牌时,$spreadsheet_id = '1TAWybckPrnWlQxBZh0ScDsFOvftwi2dvTBNGarSdY30';的电子表格可以检索值。
  • 使用服务帐户检索的访问令牌时,将返回Error 404 (Not Found)!!1

如果我的理解是正确的,请确认以下几点。

Confirmation points:

  1. 作为测试运行,请设置范围$spreadsheet_range = '';。 例如,它是$spreadsheet_range = 'Sheet1'
  2. 如果返回The caller does not have permission的错误消息,请确认如下。 1TAWybckPrnWlQxBZh0ScDsFOvftwi2dvTBNGarSdY30的电子表格是否正在共享服务帐户的电子邮件。 如果您没有将服务帐户分享到电子表格,请将client_email文件中的readsheetmar2019-08b737d1c1cb._portfolio_test.json电子邮件分享给您要访问的电子表格。
  3. 如果返回Google Sheets API has not been used in project ### before or it is disabled.的错误消息,请启用Sheets API。

如果这不是您的问题的解决方案,我道歉。

© www.soinside.com 2019 - 2024. All rights reserved.