0
votes

I am trying to pull in a Google Sheet via the PHP API Client, but I am getting a 404 on a document that exists. Below is the error, a link to my spreadsheet, and my code. The file has been given edit permissions to my user, which is the user I used to setup the json credentials via the Google Developers Console. What am I missing?

Spreadsheet:

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

Error:

[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

Code:

<?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
"The file has been given edit permissions to my user" Your using a service account I assume you mean you took the service account email address and gave it edit permissions? - DaImTo
@DaImTo Yep, in fact the service account is what I used to create the spreadsheet. - stinogle

1 Answers

0
votes

On the Handling API Errors page of the Drive API documentation

404: File not found: {fileId}

The user does not have read access to a file, or the file does not exist.

Suggested action: Report to users that they do not have read access to the file or that the file does not exist. Tell them that they should ask the owner for permission to the file.

I think you're missing some authorization scopes that causes the issue. Set scopes like https://www.googleapis.com/auth/drive for full permission (or select some items on the list in the About Authorization page of the documentation.)