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

aru*_*mar 4 php google-api google-sheets google-api-php-client

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

我收到这个致命错误:

致命错误:未捕获的异常 'Google_Service_Exception' 带有消息 '{ "error": { "code": 403, "message": "调用方没有权限", "errors": [ { "message": "调用方没有权限没有权限", "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]);
    }
  }
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

DaI*_*mTo 9

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