找不到Google表格的服务帐户退货 [英] Service Account for google sheets returns not found

查看:92
本文介绍了找不到Google表格的服务帐户退货的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to read a spreadsheet using a service account (I cannot use OAuth, which works, since the process will be running on a server to periodically check sheet data)

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

I tried several approaches. If I follow the example using oauth I can see the sheet values. However, I need the run the script without any GUI on the background.

我已经找到本教程 https://github.com/juampynr/google-spreadsheet-读者 我创建了一个项目服务帐户,添加了查看者角色,并与服务帐户电子邮件共享了电子表格.生成密钥. 看来测试程序可以连接到google服务,但是当它请求电子表格时,最终结果是找不到404".

I have found this tutorial https://github.com/juampynr/google-spreadsheet-reader I have created a projec, service account, added viewer role, shared the spreadsheet with the service account email. Generated the key. It seems that the test program can connect to the google services but the moment it request the spreadsheet the end result is "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

Error:PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message ' Error 404 (Not Found)!!1

推荐答案

  • 使用OAuth2检索的访问令牌时,$spreadsheet_id = '1TAWybckPrnWlQxBZh0ScDsFOvftwi2dvTBNGarSdY30';的电子表格可以检索值.
  • 使用通过服务帐户检索的访问令牌时,将返回Error 404 (Not Found)!!1.
    • When the access token retrieved by OAuth2 is used, the Spreadsheet of $spreadsheet_id = '1TAWybckPrnWlQxBZh0ScDsFOvftwi2dvTBNGarSdY30'; can retrieve the values.
    • When the access token retrieved by Service Account is used, Error 404 (Not Found)!!1 is returned.
    • 如果我的理解正确,请确认以下几点.

      If my understanding is correct, please confirm the following points.

      1. 作为测试运行,请设置范围$spreadsheet_range = '';.
        • 例如,它是$spreadsheet_range = 'Sheet1'.
      1. As a test run, please set the range $spreadsheet_range = '';.
        • For example, it's $spreadsheet_range = 'Sheet1'.
      • 1TAWybckPrnWlQxBZh0ScDsFOvftwi2dvTBNGarSdY30的电子表格是否正在共享服务帐户的电子邮件.
      • 如果您没有将服务帐户共享至电子表格,请共享readsheetmar2019-08b737d1c1cb._portfolio_test.json文件中的client_email电子邮件至您要访问的电子表格.
      • Whether the Spreadsheet of 1TAWybckPrnWlQxBZh0ScDsFOvftwi2dvTBNGarSdY30 is sharing the email of Service Account.
      • If you didn't share the Service Account to the Spreadsheet, please share the email of client_email in the file of readsheetmar2019-08b737d1c1cb._portfolio_test.json to the Spreadsheet you want to access.

      如果这不是解决您问题的方法,我深表歉意.

      If this was not the solution for your issue, I apologize.

      这篇关于找不到Google表格的服务帐户退货的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆