PHP Google Sheets API - Permission Denied

爷,独闯天下 提交于 2020-01-24 12:38:08

问题


Hi I've just started to study PHP and I'm attempting to use the Google sheets API.

I thought I followed their instructions and I could call and edit sheet data well but only Public Access.

When I change access status as private or Group, I encounter message below.

PHP 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"
  }
}

And I will put my codes what you would see to help.

<?php
require_once __DIR__ . '/vendor/autoload.php';

define('APPLICATION_NAME', 'Google Sheets API PHP Quickstart');
define('CREDENTIALS_PATH', __DIR__ . '/service-account-credentials.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
define('SCOPES', implode(' ', array(
        Google_Service_Sheets::SPREADSHEETS)
));

putenv('GOOGLE_APPLICATION_CREDENTIALS='.CREDENTIALS_PATH);

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Google_Service_Drive::DRIVE);

$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);

$tokenArray = $client->fetchAccessTokenWithAssertion();
$accessToken = $tokenArray["access_token"];

$service = new Google_Service_Sheets($client);

$spreadsheetId = "1qe7QwyYxWuIAYT7UUZ2s3KdDZrAHJ02uPH4VPrr-pMk";

$range = "Sheet1";
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
if (count($values) == 0) {
    print "No data found.\n";
}
print_r($values);

Is there anything to confirm in the code? or google API console?

Any information would be greatly appreciated! thanks in advance!

来源:https://stackoverflow.com/questions/39330695/php-google-sheets-api-permission-denied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!