使用新 Google API 控制台项目获取 unauthorized_client ,客户端无权使用此方法检索访问令牌

Using New Google API Console project getting unauthorized_client ,Client is unauthorized to retrieve access tokens using this method

我创建了一个 Google API 控制台项目和带有 Web 应用程序的客户端 ID type.Then 使用 OAuth 2.0 Playground - Google Developers 我授权驾驶,sheet 和日历范围使用我的客户 ID。

此外,在 G Suite 中添加和授权服务帐户客户端 ID 和范围。

我尝试使用以下示例列出驱动器文件夹中的文件

index.php

<?php
require_once 'vendor/autoload.php';
require_once 'vendor/google/apiclient/examples/templates/base.php';
$service = get_service_document();
$folderid='FOLDER_ID';


try {
   $children1 = $service->files->listFiles(array(
       'q' => "'$folderid' in parents "));
   $filearray1 = $children1;
}
catch(Exception $e){
   echo $e->getMessage();
}
print_r($children1);
exit;

function buildServiceDrive($userEmail,$service_id,$scope,$service_filename) {

   $client = new Google_Client();
   putenv("GOOGLE_APPLICATION_CREDENTIALS=".$service_filename);
   if ($credentials_file = checkServiceAccountCredentialsFile()) {
// set the location manually
       $client->setAuthConfig($credentials_file);
   }
   elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
// use the application default credentials
       $client->useApplicationDefaultCredentials();
   }
   else {
       echo missingServiceAccountDetailsWarning();
       return;
   }
   $client->setApplicationName("DRIVE");
   $client->setScopes('https://www.googleapis.com/auth/drive');
   $client->setSubject($userEmail);
   return new Google_Service_Drive($client);
}
//COMMON FUNCTION TO CREATE CALENDAR ID
function get_service_document(){

   $userstamp='user@domain.com';
   $driveService =buildServiceDrive($userstamp,'','','project-id-451a5f6b12ce.json';
   return $driveService;
}

但是我遇到了这个问题

{
 "error": "unauthorized_client",
 "error_description": "Client is unauthorized to retrieve access tokens using this method."
}

我收到这个新创建的问题 Google API 仅限控制台项目

请帮我解决这个问题。 提前致谢

当 运行 使用服务帐户进行 API 调用但未正确完成全域委派 (DWD) 或因为管理控制台中的授权未传播时,这是一个常见错误然而。

This article详细解释了DWD的过程。如果您这样做了,请等待 24 小时,它应该会起作用。如果在那之后它不起作用,那么它一定是别的东西,但据我现在可以说,DWD 过程是问题所在。

请注意:DWD 仅适用于 G Suite 客户。如果您使用的是消费者 gmail.com 帐户,则无法执行此操作。相反,您必须通过用户同意 OAuth 流程。

如果 API 客户端只有写权限并且在范围内您指定只需要只读访问权限,也可能会发生此错误。

{
   "error": "unauthorized_client",
   "error_description": "Client is unauthorized to retrieve access tokens using this method."
}