【问题标题】:Google API error, Unauthorised client using the service accountGoogle API 错误,未经授权的客户端使用服务帐户
【发布时间】:2018-07-02 06:25:37
【问题描述】:

之后我收到以下错误:

致命错误:未捕获的 Google_Service_Exception: { "error": "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;由于环境仍标记为“(Beta)”,因此是否存在任何已知问题。 (c) 是否有任何示例/教程可以指导解决我们的问题。

【问题讨论】:

    标签: php google-sheets google-api google-drive-api service-accounts


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-02-22
      • 1970-01-01
      • 2012-05-20
      • 2016-06-21
      • 1970-01-01
      • 2016-06-25
      • 2019-04-25
      • 2021-01-07
      • 1970-01-01
      相关资源
      最近更新 更多