1

in into google drive api with service account credential, to upload file for a specific user j********@gmail.com.

I create my account service credential in google developer API console. i add permission for user j****@gmail.com i authorize my app for j****@gmail.com

but sometimes autetication fails with this messagge

Google_Service_Exception

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

here's my code for 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();

1 Answers1

0

You can use the Google API Client Library for PHP to create web server applications that use OAuth 2.0 authorization to access Google APIs. Also, web applications frequently also use service accounts when making API calls, particularly when calling Cloud APIs to access project-based data rather than user-specific data. These mechanisms can be used in conjunction with user authorization in a web server application.

Regarding you error unauthorized_client, it usually occurs in case the request failed to pass validation. As stated here, if you use Oauth2 you need to have a refresh token for each user who will be using your application. You can check this tutorial on how OAuth2 works.

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59