Google API服务帐号授权错误 [英] Google API Service Account Authorization Error
问题描述
我使用 google-api-php-client 客户端使用服务帐户连接到Google云端硬盘。大多数情况下,它可以工作,但每隔一段时间(如果我每隔5到10次坐在一次刷新页面),我会收到一个 Google_Service_Exception
消息 unauthorized_client:客户未经授权使用此方法检索访问令牌
。如果存在 $ this-> drive_service-> files-> listFiles()
代码,则只会出现该错误。如果我直接使用服务帐户而不是使用 setSubject()
来模拟另一个用户,则不会发生。
I am using the google-api-php-client client to connect to Google Drive using a Service Account. Most of the time it works, but every so often (say if I sit here an refresh the page over and over it's every 5th to 10th time), I receive a Google_Service_Exception
with the message unauthorized_client: Client is unauthorized to retrieve access tokens using this method
. The error only occurs if the $this->drive_service->files->listFiles()
code is present. It does not occur if I use the Service Account directly rather than using setSubject()
to impersonate another user.
$this->client = new Google_Client();
$this->client->useApplicationDefaultCredentials();
$this->client->addScope("https://www.googleapis.com/auth/drive");
$this->client->setSubject('xxxx');
$this->drive_service = new Google_Service_Drive($this->client);
$files = $this->drive_service->files->listFiles();
有什么想法?
更新2月21日,2017:错误不再是间歇性的,每次都会发生,所以我不能再通过服务帐户连接到普通帐户。
Any thoughts?
3月10日更新,2017:看起来好像这是用户的误解。该服务帐户必须被授予全域性权限,以模仿该域的用户 - 我还没有做过。我只是按照这里。
Update Feb 21, 2017: The error is no longer intermittent, it happens every time, so I can no longer connect at all to a regular account through a service account.
推荐答案
我刚刚创建了模拟帐户时,间歇性地看到了此错误。从外观上看,在尝试创建服务之前添加10秒的延迟消除了问题。
I've seen this error intermittently when the account that's being impersonated is newly created. By the looks of it, adding a 10 second delay before trying to create the service eliminates the problem.
这篇关于Google API服务帐号授权错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!