Возникла проблема с определением областей при попытке записи с помощью API электронных таблиц Google v4 на php

Пожалуйста, помогите мне:

Я нашел эти области, необходимые для записи в электронную таблицу Google по адресу https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update

Авторизация

Требуется одна из следующих областей действия OAuth:

  • 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".

Вот полная ошибка:

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

person mohiuddin siddiquy    schedule 27.05.2017    source источник


Ответы (2)


Вы можете проверить и удалить ранее сохраненные учетные данные на ~/.credentials/drive-php-quickstart.json на основе быстрого запуска PHP :

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)
));

Затем запустите приложение, чтобы получить новый набор учетных данных.

Надеюсь это поможет.

person Mr.Rebot    schedule 27.05.2017
comment
Спасибо за ответ. Я сделал это, но получил еще одну ошибку: -------------- Неперехваченное исключение 'LogicException' с сообщением 'токен обновления должен быть передан или установлен как часть setAccessToken' в / home / muthop5 / public_html / sheet / vendor / google / apiclient / src / Google / Client.php: 267 Трассировка стека: # 0 /home/muthop5/public_html/sheet/quickstart.php(65): Google_Client- ›fetchAccessTokenWithRefreshToken (NULL) # 1 / home / muthop5 / public_html / sheet / quickstart.php (93): getClient () # 2 {main} добавлено в /home/muthop5/public_html/sheet/vendor/google/apiclient/src/Google/Client.php в строке 267 - person mohiuddin siddiquy; 28.05.2017

Чтобы добавить в электронную таблицу, убедитесь, что область видимости не только для чтения, когда вы в нее пишете. обязательно установите запрос на утверждение, так как это также вызывает проблемы.

Google_Service_Sheets::SPREADSHEETS_READONLY)

Вместо этого используйте

$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');
person Ritesh    schedule 10.06.2017