我在使用服务帐户进行身份验证时遇到了问题:
putenv('GOOGLE_APPLICATION_CREDENTIALS='/path/to/google.json');
$client = new \Google_Client();
$client->addScope(\Google_Service_Drive::DRIVE);
$client->useApplicationDefaultCredentials();
$client->setAccessType('offline');
$client->setSubject('xxx@xxx.com');但它显示的错误如下:
{
"error": "unauthorized_client",
"error_description": "Client is unauthorized to retrieve access tokens using this method."
}当我移除
$client->setSubject('xxx@xxx.com');它可以工作(我可以访问),但是还有另外一个问题,我无法在我的google驱动器中看到通过应用程序创建的任何文件(使用UI)。据我所知,这仅仅是因为我使用服务帐户,它在他自己的空间上创建文件,在那里我只能通过API访问。我需要访问文件从API和谷歌驱动器应用程序。
此外,我需要访问我的谷歌驱动器,而不登录到我的谷歌帐户(不重定向用户到谷歌登录页面)。
如果你能帮我解决这个问题,举几个例子,我会非常感激的。
发布于 2018-02-01 13:14:14
你需要记住,服务帐户不是你。一个服务帐户是一个虚拟用户,它有自己的谷歌驱动器帐户。在你上传之前,谷歌驱动器帐户上不会有任何文件。
发布于 2018-02-07 17:32:21
服务帐户用于代表用户行事。您可以将用户设置为您的帐户。这样,服务帐户代表您的利益,您可以看到一切(使用UI)通过您的应用程序创建。
要避免所面临的错误,请确保完成以下操作:
- Copy the **client\_id**. You can find it in the json file (service account credential file) or Using the Api Console, by going to Credentials > Manage Service Account then 'View Client ID'. Copy the **client\_id** to clipboard
- Launch the G-Suite Admin Console
- Go to Security>Advanced Settings>Mange API client access.
- Paste the **client\_id** in the Client Name field, add the following API scopes and click Authorize: https://www.googleapis.com/auth/admin.directory.group,https://www.googleapis.com/auth/admin.directory.user,https://www.googleapis.com/auth/calendar.readonly,https://www.googleapis.com/auth/drive
注意事项:如果步骤3没有完成,您将得到所得到的错误
参考资料:https://support.google.com/a/answer/7378726?hl=en https://github.com/sazaamout/gDrive
https://stackoverflow.com/questions/48563348
复制相似问题