1

请有人帮助我:

我发现这些范围需要在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'
));

我可以从电子表格中读取和获取数据。但是当我尝试写一些东西时,它说“请求的身份验证范围不足”。

这是完整的错误:

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
4

2 回答 2

0

~/.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)
));

然后执行应用程序以获取新的凭据集。

希望这可以帮助。

于 2017-05-27T22:49:28.020 回答
-1

要添加到电子表格,请确保在写入时范围不是只读的。确保设置批准提示,因为这也会导致问题。

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');
于 2017-06-10T10:49:30.143 回答