Having issue with define scopes while trying to write by google spreadsheet API v4 in php

488

Please someone help me:

I found these scopes needed to write in google spreadsheet at https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update

Authorization

Requires one of the following OAuth scopes:

  • https://www.googleapis.com/auth/drive
  • https://www.googleapis.com/auth/drive.file
  • https://www.googleapis.com/auth/spreadsheets

$client->setScopes(array(
    'https://www.googleapis.com/auth/drive',
    'https://www.googleapis.com/auth/drive.file',
    'https://www.googleapis.com/auth/spreadsheet'
));

I can read and get data from the spreadsheet. But when I try to write something it says "Request had insufficient authentication scopes".

Here is the full 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" } } ' in /home/muthop5/public_html/sheet/vendor/google/apiclient/src/Google/Http/REST.php:118 Stack trace: #0 /home/muthop5/public_html/sheet/vendor/google/apiclient/src/Google/Http/REST.php(94): Google_Http_REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #1 [internal function]: Google_Http_REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), 'Google_Service_...') #2 /home/muthop5/public_html/sheet/vendor/google/apiclient/src/Google/Task/Runner.php(176): call_user_func_array(Array, Array) #3 /home/muthop5/public_html/sheet/vendor/google/apiclient/src/Google/Http/REST. in /home/muthop5/public_html/sheet/vendor/google/apiclient/src/Google/Http/REST.php on line 118
392

Answer

Solution:

You might want to check and delete the previously saved credentials at~/.credentials/drive-php-quickstart.json based on the PHP Quickstart:

define('APPLICATION_NAME', 'Google Sheets API PHP Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/sheets.googleapis.com-php-quickstart.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/sheets.googleapis.com-php-quickstart.json
define('SCOPES', implode(' ', array(
  Google_Service_Sheets::SPREADSHEETS_READONLY)
));

Then execute the app to get new set of credentials.

Hope this helps.

20

Answer

Solution:

To add to speadsheet make sure scope is not readonly as you are writing to it. make sure to set the approval prompt as that also causes issues.

Google_Service_Sheets::SPREADSHEETS_READONLY)

Instead use

$scope = array('https://www.googleapis.com/auth/spreadsheets','https://www.googleapis.com/auth/drive');
    $client->addScope($scope);
    $client->setIncludeGrantedScopes(true);
    $client->setAccessType('offline');
    $client->setApprovalPrompt('force');

People are also looking for solutions to the problem: php - How to return the view from controller in laravel in this case

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.