37982

Google Drive V3, Google API client 2.0 - batch upload is failing

Batch upload fails using Google Drive V3 with master branch of API (v2.0).

I've modified https://github.com/google/google-api-php-client/blob/master/examples/batch.php with service account credentials.

The code:

include_once __DIR__ . '/../vendor/autoload.php'; include_once "templates/base.php"; echo pageHeader("Batching Queries"); // USE TRUE OR FALSE TO TOGGLE BETWEEN BATCHED AND SEQUENTIAL UPLOADS. $useBatch = true; $client = new Google_Client(); $client->setScopes([ 'https://www.googleapis.com/auth/drive', ]); 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 { exit; } $client->setSubject('some@email.com'); $service = new Google_Service_Drive($client); $client->setUseBatch($useBatch); if ($useBatch) { $batch = $service->createBatch(); } $folder = new Google_Service_Drive_DriveFile([ 'name' => 'Invoices', 'mimeType' => 'application/vnd.google-apps.folder' ]); $req = $service->files->create($folder, [ 'fields' => 'id' ]); if ($useBatch) { $result = $batch->add($req, 'newfolder'); $folder = $batch->execute()['response-newfolder']; $newFolderId = $folder->id; } else { $newFolderId = $req->id; } $uploadIDs = null; if ($useBatch) { $batch = $service->createBatch(); } for ($i=1;$i<=3;$i++) { $file = new Google_Service_Drive_DriveFile([ 'name' => $i . '.jpg', 'mimeType' => 'image/jpeg', 'parents' => [$newFolderId], ]); $req = $service->files->create($file, [ 'data' => file_get_contents('img/'.$i.'.jpg'), 'mimeType' => 'image/jpeg', 'uploadType' => 'media', 'fields' => 'id', ]); if ($useBatch) { $batch->add($req, $i); } else { $uploadIDs[] = $req->id; } } if ($useBatch) { $results = $batch->execute(); } else { print_r($uploadIDs); }

The code above will fail with "Not Found" after running last $results = $batch->execute(); (folder Invoices will be created successfully).

With $useBatch = false everything works as expected - a folder is created with three files in it.

Why is it crashing on batch upload?

Thanks!

Answer1:

Based on the Official Google Documentation, you received '404 File not found' due to user does not have read access to a file or the file does not exist. Suggested action: Report to users that they do not have read access to the file or that the file does not exist. Tell them that they should ask the owner for permission to the file.

You have to include your '$fileId' in your request. Also, you should set '$userPermission' if '$useBatch=true'.

Note: You should use the v1-branch as it is stated here: https://github.com/google/google-api-php-client

Recommend

  • How to handle generator code inside a decorator?
  • Download url not working in Google Drive API PHP
  • Google Apps Script Error while creating document using execution API PHP Client
  • Why can't I retrieve the users email from Google Plus API after getting permission
  • Error when creating google drive folder using sample PHP code
  • Google Drive PHP API Download file
  • How to get UBER Access token using authorization code in Login Callback
  • App Closes On Clicking Google SIgn In Button
  • How to read a certificate from a USB token in C#
  • Client-Side: Accessing Windows Azure Drive?
  • Docker container for google cloudML on compute engine - authenticating for mounting bucket
  • Youtube upload API and cordova / phonegap
  • How to get ID of changed file on Google Drive
  • Sencha Touch 2.1 native (android) app not getting json from remote (it works on PC)
  • Django: DRY principle and UserPassesTestMixin
  • Jquery Show & ScrollTop (or ScrollTo)
  • CSS: How to fix overlapping divs
  • Change navbar in bootstrap if user login
  • Android Chronometer starts and stops but carries on counting when stopped
  • Cannot page through all results using nextPageToken on YouTube search API v3
  • Django return user model id with L
  • Laravel: Getting Session ID oddly truncates when using foreach
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Optimizing database types to compact database (SQLite)
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Cross-Platform Protobuf Serialization
  • Do I've to free mysql result after storing it?
  • How to stop GridView from loading again when I press back button?
  • Turn off referential integrity in Derby? is it possible?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?