404 通过 PHP API 获取 Google 表格时



我正在尝试通过PHP API客户端拉入Google Sheet,但是我在现有的文档上得到了404。下面是错误、指向我的电子表格的链接和我的代码。该文件已被授予我的用户编辑权限,这是我用来通过 Google 开发人员控制台设置 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(GuzzleHttpPsr7Response), Object(GuzzleHttpPsr7Request), 'Google_Service_...')
#1 /Applications/MAMP/htdocs/lp/vendor/google/apiclient/src/Google/Task/Runner.php(181): Google_Http_REST::doExecute(Object(GuzzleHttpClient), Object(GuzzleHttpPsr7Request), '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);
?>

在云端硬盘 API 文档的处理 API 错误页面上

404:找不到文件:{文件标识}

用户对文件没有读取访问权限,或者该文件不存在。

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

我认为您缺少一些导致问题的授权范围。将范围(如 https://www.googleapis.com/auth/drive(设置为完全权限(或在文档的"关于授权"页中选择列表中的某些项目。

相关内容

  • 没有找到相关文章

最新更新