2

i have a Problem with the Spreadsheed api and the "scopes". With these script i want to update Cells on a Sheet.

I do not work with composer ich have just download the package in intereating it. The Token is already there and the error is from these row:

"$response = $service->spreadsheets_values->get($spreadsheetId, $range);"

<?php session_start(); require_once __DIR__.'/vendor/autoload.php'; $client = new Google_Client(); $client->setAuthConfig('oauth-credentials.json'); $client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY); if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { $client->setAccessToken($_SESSION['access_token']); echo "<pre>"; $service = new Google_Service_Sheets($client); $spreadsheetId = 'xxx'; $range = 'Tabellenblatt1!A2:E'; $response = $service->spreadsheets_values->get($spreadsheetId, $range); $values = $response->getValues(); if (count($values) == 0) { print "No data found.\n"; } else { print "Name, Major:\n"; foreach ($values as $row) { // Print columns A and E, which correspond to indices 0 and 4. printf("%s, %s <br>", $row[0], $row[4]); } } } else { $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/api/oauth2callback.php'; header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL)); } ?> 

These Code brings the following Error

Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "code": 403, "message": "Request had insufficient authentication scopes.", "errors": [ { "message": "Request had insufficient authentication scopes.", "domain": "global", "reason": "forbidden" } ], "status": "PERMISSION_DENIED" } } 
1
  • Nobody an idea? Commented Jun 16, 2017 at 6:30

2 Answers 2

2

You have this scope defined:
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);

For accessing a spreadsheet value:
$response = $service->spreadsheets_values->get($spreadsheetId, $range);

You should have:
$client->addScope(Google_Service_Sheets::SPREADSHEETS_READONLY);
or
$client->addScope(Google_Service_Sheets::SPREADSHEETS);

Source:
https://developers.google.com/identity/protocols/googlescopes#sheetsv4

Sign up to request clarification or add additional context in comments.

Comments

1

You Must Update the scope as @random425 said,

but after that delete the Token.json. that will start the process of verification again what will give you new token with new scope that you have changed to.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.