3
votes

Can you please help me with this ?

I am using PHP GOOGLE API V2.2.2

My code works using a gserviceaccount created for it, BUT, when I am trying manipulate the files on my personal GOOGLE DRIVE I have this error...

I tried following the steps shown here: Github issue 801

It went just fine and my project is shown on my authorized client list

Here is my code to create a folder inside my google drive:

  function NovaPasta() {


    require_once APPPATH . 'third_party/google-api-php-client-2.2.2/vendor/autoload.php';

    putenv('GOOGLE_APPLICATION_CREDENTIALS='. APPPATH . 'third_party/google-api-php-client-2.2.2/Asist-428f3540df9a.json');
    $client = new Google_Client();
    $client->useApplicationDefaultCredentials();
    //$client->setAuthConfig(APPPATH . 'third_party/google-api-php-client-2.2.2/Asist-428f3540df9a.json');
    $client->addScope("https://www.googleapis.com/auth/drive");
    $client->setSubject('[email protected]');

    $driveService = new Google_Service_Drive($client);

    $fileMetadata = new Google_Service_Drive_DriveFile(array(
        'name' => 'new folder name',
        'mimeType' => 'application/vnd.google-apps.folder'));
    $file = $driveService->files->create($fileMetadata, array(
        'fields' => 'id'));
    printf("Folder ID: %s\n", $file->id);
}

Here is the error:

An uncaught Exception was encountered Type: Google_Service_Exception

Message: { "error": "unauthorized_client", "error_description": "Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested." }

enter image description here

1
Are you trying to access only your drive account? Will you never access another users account except your own?DaImTo
Could you tell us what scopes you have used?jess

1 Answers

0
votes

If you want to use service account then uncomment $client->setAuthConfig point to json file and remove line $client->useApplicationDefaultCredentials();

View more example at https://github.com/googleapis/google-api-php-client/tree/master/examples

$client = new Google_Client();
$client->setAuthConfig('service-account.json');
$client->setScopes(['https://www.googleapis.com/auth/drive']);
$service = new Google_Service_Drive($client);
$fileMetadata = new Google_Service_Drive_DriveFile(array('name' => 'Google_Service_Folder','mimeType' => 'application/vnd.google-apps.folder'));
$file = $service->files->create($fileMetadata, array('fields' => 'id'));