3

I am trying to upload files to google drive using service account, but i am getting error like

(403) Insufficient Permission error

Although fetching list of files from drive is working fine.

require_once 'google-api-php-client-master/src/Google/autoload.php';
require_once 'google-api-php-client-master/src/Google/Service.php';
session_start();

$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$service_account_name = '251181790499-1en2vs6jjovga1egu15tphm1m7g44m60@developer.gserviceaccount.com';
$key_file_location = 'key.p12'; //key.p12

$key = file_get_contents($key_file_location);;

if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
    $client->setAccessToken($_SESSION['access_token']);
    //$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
    //$client->setScopes(array('https://www.googleapis.com/auth/drive'));
    $cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/drive'),
    $key);

    $client->setAssertionCredentials($cred);
    $client->setAccessType("offline");

    $file = new Google_Service_Drive_DriveFile();
    $file->setTitle(uniqid().'.jpg');
    $file->setDescription('A test document');
    $file->setMimeType('image/jpeg');

    $data = file_get_contents('a.jpg');

    $service = new Google_Service_Drive($client);
    $createdFile = $service->files->insert($file, array(
          'data' => $data,
          'mimeType' => 'image/jpeg',
          'uploadType' => 'multipart'
        ));

    print_r($createdFile);

} 
else
{
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/googledrive/oauth2callback.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

I have reviewed similar questions in forum and tried suggestions but nothing seems to work.

Is there anything wrong with code?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
user1750791
  • 83
  • 1
  • 7
  • Did you grant the service account permission to access the file? – Linda Lawton - DaImTo Oct 12 '15 at 07:40
  • Under the permissions tab Service account has by default "Can Edit" permissions. Is there anything i have to do? – user1750791 Oct 12 '15 at 07:45
  • $key_file_location <--- should probably be the full path to the file. – Linda Lawton - DaImTo Oct 12 '15 at 07:48
  • i can try, but does it matter if both files(key file and the file where code is) are on same path? – user1750791 Oct 12 '15 at 07:50
  • what does file_get_contents return? Is it finding the file? that scope should be giving you full access and you are uploading to the service accounts google drive account so my first comment is invalid you should have access. It must be an authentication issue. – Linda Lawton - DaImTo Oct 12 '15 at 07:52
  • file_get_contents returning content of key file which is nonreadable. I can fetch list of content from google drive also. Is there different permissions required to upload – user1750791 Oct 12 '15 at 07:58
  • https://developers.google.com/drive/web/scopes the one you are using should give you full power. – Linda Lawton - DaImTo Oct 12 '15 at 08:11
  • Thanks Dalm, seems to be caching issue :( I got link of file in response but it's not visible in google drive now – user1750791 Oct 12 '15 at 08:20
  • Remember you are uploading to the service accounts google drive account. If you want to be able to see it from your own Google drive account you are going to have to do an insert of the permissions. to give yourself access – Linda Lawton - DaImTo Oct 12 '15 at 08:22
  • 2
    https://developers.google.com/drive/v2/reference/permissions/insert then you can grant yourself access to the file on the service accounts drive account. – Linda Lawton - DaImTo Oct 12 '15 at 08:23
  • ok got it, So once i set permissions files would be visible under "Shared with me" folder on drive instead of "My Drive" – user1750791 Oct 12 '15 at 08:35

1 Answers1

4

Change your Google Drive Scope to

Google_Service_Drive::DRIVE

in PHP

$client->addScope(Google_Service_Drive::DRIVE);
powtac
  • 40,542
  • 28
  • 115
  • 170