繁体   English   中英

有时访问google drive api服务帐户失败

[英]accessing google drive api service account fails sometimes

进入带有服务帐户凭证的google drive api,为特定用户j********@gmail.com上传文件。

我在google developer API控制台中创建了帐户服务凭据。 我为用户j****@gmail.com添加权限我授权我的应用程序为j****@gmail.com

但是有时候这种混乱会导致无知

Google_Service_Exception

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

这是我的auth代码

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->setSubject('j******@gmail.com');
$client->setScopes(['https://www.googleapis.com/auth/drive']);
$service = new Google_Service_Drive($client);
$files = $service->files->listFiles();

您可以使用Google API客户端库来创建使用OAuth 2.0授权访问Google API的Web服务器应用程序。 此外,Web应用程序在进行API调用时也经常使用服务帐户 ,尤其是在调用Cloud API以访问基于项目的数据而不是用户特定数据时。 这些机制可以与Web服务器应用程序中的用户授权结合使用。

关于你的错误unauthorized_client ,它通常发生在请求未能通过验证的情况下。 如前所述在这里 ,如果你使用的OAuth2你需要有谁将会使用你的应用程序的每个用户的刷新令牌。 您可以查看本教程 ,了解OAuth2的工作原理。

暂无
暂无

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

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