【问题标题】:Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested客户端未授权使用此方法检索访问令牌,或者客户端未授权任何请求的范围
【发布时间】:2019-04-24 03:16:20
【问题描述】:

你能帮我解决这个问题吗?

我正在使用 PHP GOOGLE API V2.2.2

我的代码使用为其创建的 gserviceaccount 工作,但是,当我尝试在我的个人 GOOGLE DRIVE 上操作文件时,我遇到了这个错误...

我尝试按照此处显示的步骤操作:Github issue 801

一切顺利,我的项目显示在我的authorized client list

这是我在我的谷歌驱动器中创建文件夹的代码:

  function NovaPasta() {


    require_once APPPATH . 'third_party/google-api-php-client-2.2.2/vendor/autoload.php';

    putenv('GOOGLE_APPLICATION_CREDENTIALS='. APPPATH . 'third_party/google-api-php-client-2.2.2/Asist-428f3540df9a.json');
    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();
    //$client->setAuthConfig(APPPATH . 'third_party/google-api-php-client-2.2.2/Asist-428f3540df9a.json');
    $client->addScope("https://www.googleapis.com/auth/drive");
    $client->setSubject('xxxx@gmail.com');

    $driveService = new Google_Service_Drive($client);

    $fileMetadata = new Google_Service_Drive_DriveFile(array(
        'name' => 'new folder name',
        'mimeType' => 'application/vnd.google-apps.folder'));
    $file = $driveService->files->create($fileMetadata, array(
        'fields' => 'id'));
    printf("Folder ID: %s\n", $file->id);
}

这是错误:

遇到未捕获的异常 类型:Google_Service_Exception

消息:{“error”:“unauthorized_client”,“error_description”:“客户端未授权使用此方法检索访问令牌,或者客户端未授权任何请求的范围。” }

【问题讨论】:

  • 您是否仅尝试访问您的驱动器帐户?除了您自己的帐户之外,您永远不会访问其他用户帐户吗?
  • 你能告诉我们你用过什么范围吗?

标签: php google-api google-drive-api google-api-php-client


【解决方案1】:

如果您想使用服务帐户,请取消注释 $client->setAuthConfig 指向 json 文件并删除行 $client->useApplicationDefaultCredentials();

https://github.com/googleapis/google-api-php-client/tree/master/examples查看更多示例

$client = new Google_Client();
$client->setAuthConfig('service-account.json');
$client->setScopes(['https://www.googleapis.com/auth/drive']);
$service = new Google_Service_Drive($client);
$fileMetadata = new Google_Service_Drive_DriveFile(array('name' => 'Google_Service_Folder','mimeType' => 'application/vnd.google-apps.folder'));
$file = $service->files->create($fileMetadata, array('fields' => 'id'));

【讨论】:

    猜你喜欢
    • 2020-04-30
    • 2018-12-01
    • 2022-09-19
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 2019-08-14
    相关资源
    最近更新 更多