php - Google Api Php 客户端 - 电子表格权限错误

标签 php google-sheets google-api google-api-php-client

我正在使用 Google PHP 客户端访问电子表格数据。

我收到这个 fatal error :

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]);
    }
  }

如何解决这个问题?

最佳答案

获取服务帐户电子邮件地址,并像与任何其他用户一样与其共享工作表。然后它将可以访问工作表

关于php - Google Api Php 客户端 - 电子表格权限错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44585903/

相关文章:

google-sheets - Google 表格 "array_agg"... 或同等内容? (加入聚合)

php - 我正在访问日期为 : how to assign a value when a particular date is not present? 的记录

php - 检查MySQL语句是读还是写

php - Laravel 如何知道 Request::wantsJson 是对 JSON 的请求?

javascript - 如何管理 Google 登录 session (Google 登录 JavaScript 客户端)

javascript - 您无权执行此操作。无效的访问 token DialogFlow v2

mysql - Youtube API : Get subscribers counts of a million channels

php - 尝试使用 jQuery 从数据库获取信息

java - 未经授权收集Google表格信息

java - Android - 谷歌电子表格 API