简体   繁体   English

google-api-php-client“错误”:“unauthorized_client”

[英]google-api-php-client “error”: “unauthorized_client”

We are trying to build a service to export google drive files for in our gsuite domain. 我们正在尝试构建一个服务,以便在我们的gsuite域中导出谷歌驱动器文件。

We're working with the https://github.com/google/google-api-php-client 我们正在使用https://github.com/google/google-api-php-client

We made a service user, downloaded and saved the user credentials and granted domain-wide-access to the service user. 我们创建了一个服务用户,下载并保存了用户凭据,并授予了对服务用户的域范围访问权限。

With the simplest example like below (like in every doc, i found): 使用下面最简单的示例(就像在每个文档中,我发现):

putenv('GOOGLE_APPLICATION_CREDENTIALS='/service-account-credentials.json');

$client = new \Google_Client();
$client->setAuthConfig('service-account-credentials.json');

$client->setScopes('https://www.googleapis.com/auth/drive.file');

$client->useApplicationDefaultCredentials();
$client->setSubject('admin@mydomain.com');

$service = new \Google_Service_Drive($client);

$service->files->listFiles(); 

as soon as we include the line $client->setSubject('admin@mydomain.com'); 一旦我们包含$client->setSubject('admin@mydomain.com'); it's dropping the following error: 它正在删除以下错误:

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

has anyone an idea, or can share a code sample for this problem. 有任何想法,或可以共享此问题的代码示例。 It's a real blocker for us and for now I don't have any idea why this error appears 这对我们来说是一个真正的阻碍者,现在我不知道为什么会出现这个错误

I finally made it 我终于做到了

After a couple of trys I realized I was delegating domain-wide-access to the wrong Client-ID. 经过几次尝试后,我意识到我正在委托对域错误的Client-ID进行域范围访问。 I always thought this should be the same as the "client_email" I'm using in the script. 我一直认为这应该与我在脚本中使用的“client_email”相同。 But it is really (really, really, really) important, that this is the "client_id" (as mentioned in the documentation by the way). 但它确实(真的,真的,非常重要),这就是“client_id”(正如文档中提到的那样)。 This is not an email or a string it's just a simple number you get when you create a key for the Service Account. 这不是电子邮件或字符串,只是您为服务帐户创建密钥时获得的简单数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM