404通过PHP API获取Google Sheet时

时间:2016-02-15 08:00:47

标签: php google-api google-drive-api google-api-php-client google-api-client

我正试图通过PHP API Client提取Google表格,但我在存在的文档上获得了404。以下是错误,指向我的电子表格的链接以及我的代码。该文件已被授予我的用户编辑权限,该用户是我用来通过Google Developers Console设置json凭据的用户。我错过了什么?

电子表格:

https://docs.google.com/spreadsheets/d/1ZYa536OzU3aJs-jvxTDdKV9rGF9BiPLnKRp2wJL8va0/edit#gid=0

错误:

[15-Feb-2016 08:46:04 Europe/Berlin] PHP Fatal error:  Uncaught Google_Service_Exception: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "File not found: 1ZYa536OzU3aJs-jvxTDdKV9rGF9BiPLnKRp2wJL8va0",
    "locationType": "other",
    "location": "file"
   }
  ],
  "code": 404,
  "message": "File not found: 1ZYa536OzU3aJs-jvxTDdKV9rGF9BiPLnKRp2wJL8va0"
 }
}
 in /Applications/MAMP/htdocs/lp/vendor/google/apiclient/src/Google/Http/REST.php:129
Stack trace:
#0 /Applications/MAMP/htdocs/lp/vendor/google/apiclient/src/Google/Http/REST.php(88): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#1 /Applications/MAMP/htdocs/lp/vendor/google/apiclient/src/Google/Task/Runner.php(181): Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...')
#2 /Applications/MAMP/htdocs/lp/vendor/google/apiclient/src/Google/Http/REST.php(57): Google_Task_Runner->run()
#3 /Applications/MAMP/htdocs/lp/vendor/google/a in /Applications/MAMP/htdocs/lp/vendor/google/apiclient/src/Google/Http/REST.php on line 129

代码:

<?php
require_once realpath(dirname(__FILE__) . '/vendor/autoload.php');
include_once "google-api-php-client/examples/templates/base.php";
$client = new Google_Client();
putenv("GOOGLE_APPLICATION_CREDENTIALS=service-account-credentials.json");
if ($credentials_file = checkServiceAccountCredentialsFile()) {
    $client->setAuthConfig($credentials_file);
} elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
  // use the application default credentials
  $client->useApplicationDefaultCredentials();
} else {
  echo missingServiceAccountDetailsWarning();
  exit;
}
$client->setApplicationName("local-porch-1222");
$client->setScopes(['https://www.googleapis.com/auth/drive','https://spreadsheets.google.com/feeds']);
// The file ID was copied from a URL while editing the sheet in Chrome
$fileId = '1ZYa536OzU3aJs-jvxTDdKV9rGF9BiPLnKRp2wJL8va0';
// Access Token is used for Steps 2 and beyond
$tokenArray = $client->fetchAccessTokenWithAssertion();
$accessToken = $tokenArray["access_token"];
// Section 1: Uncomment to get file metadata with the drive service
// This is also the service that would be used to create a new spreadsheet file
$service = new Google_Service_Drive($client);
$results = $service->files->get($fileId);
var_dump($results);
?>

1 个答案:

答案 0 :(得分:0)

在Drive API文档的Handling API Errors页面

  

404:找不到文件:{fileId}

     

用户没有文件的读取权限,或文件不存在。

     

建议的操作:向用户报告他们对文件没有读取权限或文件不存在。告诉他们他们应该要求所有者获得该文件的许可。

我认为你错过了一些导致问题的授权范围。设置https://www.googleapis.com/auth/drive等范围以获得完全权限(或者在文档的About Authorization页面中选择列表中的某些项目。)