Using New Google API Console project getting unauthorized_client ,Client is unauthorized to retrieve access tokens using this method

Viewed 3696

I created a Google API Console project and client ID with web application type.Then Using OAuth 2.0 Playground - Google Developers I authorized to drive, sheet and calendar scopes using my client id.

Also, Service account client id and scopes added and authorized in G Suite.

I tried to list files in a folder in the drive using the below sample

index.php

<?php
require_once 'vendor/autoload.php';
require_once 'vendor/google/apiclient/examples/templates/base.php';
$service = get_service_document();
$folderid='FOLDER_ID';


try {
   $children1 = $service->files->listFiles(array(
       'q' => "'$folderid' in parents "));
   $filearray1 = $children1;
}
catch(Exception $e){
   echo $e->getMessage();
}
print_r($children1);
exit;

function buildServiceDrive($userEmail,$service_id,$scope,$service_filename) {

   $client = new Google_Client();
   putenv("GOOGLE_APPLICATION_CREDENTIALS=".$service_filename);
   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->setApplicationName("DRIVE");
   $client->setScopes('https://www.googleapis.com/auth/drive');
   $client->setSubject($userEmail);
   return new Google_Service_Drive($client);
}
//COMMON FUNCTION TO CREATE CALENDAR ID
function get_service_document(){

   $userstamp='user@domain.com';
   $driveService =buildServiceDrive($userstamp,'','','project-id-451a5f6b12ce.json';
   return $driveService;
}

But I got this issue

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

I m getting this issues newly created Google API Console project only

Please help me to solve this. Thanks in advance

2 Answers

This error could also occur if API client only have write permissions and in scope you specify that you only need readonly access.

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