简体   繁体   中英

PHP Google Sheets API - Permission Denied

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!

File->Share->Advanced and add the service account

eg: your@email.iam.gserviceaccount.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM