skip to Main Content

I am trying to upload a zip file to google drive with a php script as a cron job on Fedora 33 x86-64. I have been tried several different things but I am not having any luck with this at this point. I have created a service account and downloaded the credentials, created the oauth concent screen, installed the Google Drive API, oauth client id, etc… Anyway when I run the script from the shell I get a very strange. Any help would be appreciated.

<?php
    require_once 'vendor/autoload.php';
    $googleClient = new Google_Client();
    $googleClient->setAuthConfig('***************************.json');
    $googleClient->addScope(Google_Service_Drive::DRIVE);
    $googleClient->setApplicationName('******************');
    $googleClient->setAccessType('offline');
    $googleClient->getAccessToken();
    $drive = new Google_Service_Drive($googleClient);
    $file = new Google_Service_Drive_DriveFile($googleClient);
    $file->setName('Mysql-Backup.zip');
    $file->setMimeType('application/zip');
    try {
      $return = $drive->files->create($file, array('data' => file_get_contents("Mysql-Backup.zip"), 'uploadType' => 'multipart', 'supportsAllDrives' => 'true'));
      echo "Sucessful upload of " . $file['name'] . ".";
    }
    catch (Exception $exc) {
      echo $exc->getMessage() . "n";
    }
?>

Forgive me if I am not doing this correctly both the code and posting on stackoverflow.com. Ok I have tried as you suggested listing the files and you are indeed correct that the files were uploaded to my service account instead of my personal drive. I have also tried your suggestion of sharing a folder from my personal account to the service account using the web interface. However when I try again it still isn’t working. This gives me an error "PHP Fatal error: Uncaught Error: Call to undefined method Google_Service_Drive_Resource_Permissions::insert() in /scripts/google-drive/google-drive.php:25". I have refereed to https://developers.google.com/resources/api-libraries/documentation/drive/v3/php/latest/class-Google_Service_Drive_Permission.html and https://developers.google.com/drive/api/v2/reference/permissions/insert#php but still I don’t understand what I am missing. I don’t know if this is frowned upon but I am willing to paypal someone if they have a currant working php class with instructions. The documentation for this is all over the place the old stuff doesn’t work and the new stuff doesn’t work. I am banging my head in at this point.

<?php
require_once 'vendor/autoload.php';
$googleClient = new Google_Client();
$googleClient->setAuthConfig('****');
$googleClient->addScope("https://www.googleapis.com/auth/drive.file");
$googleClient->setApplicationName('MySQL-Backup');
$googleClient->setAccessType('offline');
$googleClient->getAccessToken();
$drive = new Google_Service_Drive($googleClient);
$file = new Google_Service_Drive_DriveFile($googleClient);
$file->setName('Mysql-Backup.zip');
$file->setMimeType('application/zip');
$file->setParents('****');
try 
    {
    $return = $drive->files->create($file, array('data' => file_get_contents("Mysql-Backup.zip"), 'uploadType' => 'multipart', 'supportsAllDrives' => 'true'));
    echo "Sucessful upload of " . $file['name'] . " File ID:" . $return->id . "n";
    $newPermission = new Google_Service_Drive_Permission();
    //$newPermission->setValue('***');
    $newPermission->setType('user');
    $newPermission->setRole('editor');
    echo $return->id . "n";
    try 
        {
        $drive->permissions->insert($return->id, $newPermission);
        } 
    catch (Exception $exc)
        {
        echo 'Caught exception: ',  $exc->getMessage(), "n";
        }
    }
catch (Exception $exc) 
    {
    echo 'Caught exception: ',  $exc->getMessage(), "n";
    }
$optParams = array('fields' => '*');
$myfiles = $drive->files->ListFiles($optParams);
foreach( $myfiles as $k => $file )
    {
    echo "{$file['name']} - {$file['id']} ---- " . $file['mimeType'] . "n";
    try
        {
        // subfiles
        $sub_files = $drive->files->listFiles(array('q' => "'{$file['id']}' in parents"));
        foreach( $sub_files as $kk => $sub_file ) 
            {
            echo "{$sub_file['name']} - {$sub_file['id']}  ---- " . $sub_file['mimeType'] . "n";
            }
        } 
    catch (Exception $exc)
        {
        echo 'Caught exception: ',  $exc->getMessage(), "n";
        }
    }   
?>
   

3

Answers


  1. Your message states that it was uploaded. I suspect that if you do a file.list

    $optParams = array(
      'fields' => '*'
    );
    $service->files->ListFiles($optParams);
    

    You will find that the file has in fact been uploaded.

    Your issue is where the file has been uploaded. Your question should be "Where was my file uploaded?"

    You need to remember that service accounts are dummy users they have their own google drive account. You have uploaded the file to the service accounts drive account. The only way for you to see files uploaded there is programmaticlly by doing the file.list i have sugested above.

    Try sharing a directory on your personal drive account with the service account email address like you would share with any other user, through the web interface. Then when you upload the file set parents = folder id of your folder in your drive account. The file will be uploaded to your account instead of the account own by the service account.

    Just remember to set the permissions on the file after you uploaded it as the service account uploaded the file it will be the owner of the file.

    Login or Signup to reply.
  2. I suggest you follow this https://packagist.org/packages/miqoo1996/google-drive

    There are all the functions in examples for easily implementing Google Drive on your platform.

    Thanks a lot.

    Login or Signup to reply.
  3. How do you set parents?

    If you do: $file->setParents('the id of the folder');, that won’t work.

    The right way is: $file->setParents(array('the id of the folder'));

    Also make sure you are using the folder id and not the name.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search