通过使用新的Google API控制台项目获取authenticated_client,客户端被授权使用此方法来检索访问令牌 [英] Using New Google API Console project getting unauthorized_client ,Client is unauthorized to retrieve access tokens using this method

查看:89
本文介绍了通过使用新的Google API控制台项目获取authenticated_client,客户端被授权使用此方法来检索访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了具有Web应用程序类型的Google API控制台项目和客户端ID.然后使用 OAuth 2.0 Playground-Google开发人员我已授权使用我的客户ID来驱动,工作表和日历范围.

I created a Google API Console project and client ID with web application type.Then Using OAuth 2.0 Playground - Google Developers I authorized to drive, sheet and calendar scopes using my client id.

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

Also, Service account client id and scopes added and authorized in G Suite.

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

I tried to list files in a folder in the drive using the below sample

index.php

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控制台项目中遇到此问题

I m getting this issues newly created Google API Console project only

请帮助我解决此问题. 预先感谢

Please help me to solve this. Thanks in advance

推荐答案

这是使用服务帐户运行API调用但未正确完成域范围委派(DWD)或由于管理员授权而导致的常见错误控制台尚未传播.

This is a common error when running an API call with a service account but not properly completing the domain-wide delegation (DWD) or because the authorization in the admin console has not propagated yet.

本文详细说明了DWD的过程.如果您这样做了,请等待24小时,它应该可以正常工作.如果那之后不起作用,那一定是其他原因了,但据我现在所说,DWD程序就是问题所在.

This article explains in details the process of DWD. If you have done that, wait 24 hours and it should work. If it doesn't work after that, then it must be something else but as far as I can say right now, the DWD process is the issue.

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

PLEASE NOTE: DWD is available only to G Suite customers. If you are using a consumer gmail.com account, you won't be able to do this. Instead, you'll have to go through the user consent OAuth flow.

这篇关于通过使用新的Google API控制台项目获取authenticated_client,客户端被授权使用此方法来检索访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆