谷歌 API 错误,未经授权使用服务帐号的客户端



之后我收到以下错误:

致命错误: 未捕获 Google_Service_Exception: { "错误": "unauthorized_client", "error_description": "客户端未经授权使用此方法检索访问令牌。">

对于谷歌管理员设置,我们点击以下链接: https://developers.google.com/api-client-library/php/auth/service-accounts

我们正在使用Restapi.php

require __DIR__ . '/vendor/autoload.php';
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
putenv('GOOGLE_APPLICATION_CREDENTIALS=client_secret.json');
$client = new Google_Client();
$client->setApplicationName('rwmp');
$client->setAccessType("offline");        // offline access
$client->useApplicationDefaultCredentials();
$client->setSubject('user@myconference.center');

$client->setScopes([Google_Service_Sheets::SPREADSHEETS]);
$service = new Google_Service_Sheets( $client );
$spreadsheetId = '1WtuEXGm8oWxet6t9UcPU-mi7lgif1sqeTcmc9DO-_zs';
$range = 'A:G';
$response = $service->spreadsheets_values->get($spreadsheetId,$range);
$values = $response->getValues();

(a( 我们使用的是 G-Suite "基本版";这有什么限制吗? (b( 我们从 PHP (7.0( 环境调用 G-Suite API;是否有任何已知问题,因为环境仍标记为"(测试版("。 (c( 是否有任何示例/教程可以提供解决我们问题的指针。

我已经有了解决方案,我们必须从管理配置中选择范围并使用它。

ex:$client->setScopes(array('https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/spreadsheets.readonly'((;

相关内容

最新更新