简体   繁体   中英

Files updates no longer works using the google-api-php-client

I have a very small script that uploads and / or update files (wrote this about 1 year ago, borrowed 80% of the lines from the examples)

No major changes in the code since march (using 1.0.0-alpha) but mid-may the file updates stopped working, raising an Internal Server Error , i upgraded to 1.0.4-beta with no success :

PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with message 'Error calling PUT https://www.googleapis.com/upload/drive/v2/ [...] (500) Internal Error' in 
google-api-php-client-1.0.4-beta/src/Google/Http/REST.php:80

code:

$client->setDefer(true);
$request = $service->files->update($update_id,$file);

$media = new Google_Http_MediaFileUpload(
    $client,
    $request,
    'text/plain',
    null,
    true,
    $chunkSizeBytes
);
$media->setFileSize(filesize($csvfile))
$status = false;
$handle = fopen($csvfile, "rb");
while (!$status && !feof($handle)) {
    $chunk = fread($handle, $chunkSizeBytes);
    $status = $media->nextChunk($chunk);
}

File inserts (HTTP POST) are still working (using the same code for uploading the chunks)

any ideas ?

I can update a file ( previously created or copied from a template ) with this code :

     (...)
     require_once 'src/Google/Client.php';
     require_once 'src/Google/Service/Oauth2.php';
     require_once 'src/Google/Service/Drive.php';    
     (...)

     $client = new Google_Client();
     // set scopes ...
     (...)

     $GDrive_service = new Google_Service_Drive($client);
     (...)


     // then I set parameters 
        (...)
        $newTitle = $title ;
        $newDescription = $description ;
        $newMimeType = 'text/csv' ;
        $newFileName = $filename ;
        $service = $GDrive_service ;
        (...)



$updatedFile = updateFile($service, $fileId, $newTitle, $newDescription, $newMimeType, $newFileName, $newRevision) ;


function updateFile($service, $fileId, $newTitle, $newDescription, $newMimeType, $newFileName, $newRevision) {
  try {
    // First retrieve the file from the API.
    $file = $service->files->get($fileId);

    // File's new metadata.
    $file->setTitle($newTitle);
    $file->setDescription($newDescription);
    $file->setMimeType($newMimeType);

    // File's new content.
    $data = file_get_contents($newFileName);
    $convert = 'true' ;

    $additionalParams = array(
      'uploadType' => 'multipart',
      'newRevision' => $newRevision,
      'data' => $data,
      'mimeType' => $newMimeType,
      'convert' => $convert,
    );

    // Send the request to the API.
    $updatedFile = $service->files->update($fileId, $file, $additionalParams);
    return $updatedFile;
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
}


(...)
// this is info of the updated file 
$fileId = $updatedFile->getId() ;
// link of file
$cF_link = $updatedFile->alternateLink ;
// pdf version
$enllacos = $updatedFile->exportLinks ;
$cF_PDF_link = $enllacos['application/pdf'] ;
(...)

This code is working with the 1.0.5-beta php client

Sergi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM