rest - Google Api for PHP (Drive API) Export as .pdf uploaded .docx file -
i cannot obtain stable script when try upload docx file google drive, , download file pdf.
code:
//google api require_once('vendor/autoload.php'); putenv('google_application_credentials='.__dir__.'/2ab4ece19bd5.json'); $client = new google_client(); $client->setapplicationname('sp-gen'); $client->setscopes(array('https://www.googleapis.com/auth/drive')); $client->useapplicationdefaultcredentials(); $service = new google_service_drive($client); $filemetadata = new google_service_drive_drivefile(array( 'name' => '281e2399740c88957143507721bd0f25.docx', 'mimetype' => 'application/vnd.google-apps.document' )); $content = file_get_contents('281e2399740c88957143507721bd0f25.docx'); $file = $service->files->create($filemetadata, array( 'data' => $content, 'mimetype' => 'application/vnd.google-apps.document', 'uploadtype' => 'multipart', 'fields' => 'id') ); $content = $service->files->export($file->id, 'application/pdf', array( 'alt' => 'media' )); file_put_contents(str_replace('.docx', '.pdf', '281e2399740c88957143507721bd0f25.docx'), $content->getbody()->getcontents()); this code works in.. 20-30% of uses. sometimes, $service->files->export() return error code 500 in many cases request return normal response (200) content-length 0.
am doing wrong? or should kind of loop, try download file until success?
ok. spend 2 days looking solution , came several conclusions:
- using server-to-server authentication, must create service account, "separate" google drive account. that's means if create file via script, file created on service account, , can access file via api.
you can assign permissions created file, connect real google account file, , can access file via google drive. can not transfer ownership file service account google account, because google php api @ moment not have proper method. or not see way setup this. class summary.
key use exponential backoff. in other words try unless success. ( ͡° ͜ʖ ͡°)
code:
//google api require_once('vendor/autoload.php'); putenv('google_application_credentials='.__dir__.'/2ab4ece19bd5.json'); $client = new google_client(); $client->setapplicationname('sp-gen'); $client->setscopes(array('https://www.googleapis.com/auth/drive')); $client->useapplicationdefaultcredentials(); $service = new google_service_drive($client); $filemetadata = new google_service_drive_drivefile(array( 'name' => '281e2399740c88957143507721bd0f25.docx', 'mimetype' => 'application/vnd.google-apps.document' )); $content = file_get_contents('281e2399740c88957143507721bd0f25.docx'); $file = $service->files->create($filemetadata, array( 'data' => $content, 'uploadtype' => 'multipart' ); //create new permission $newpermission = new google_service_drive_permission(); //set email of account, have access file. $newpermission->setemailaddress('<email here>'); //must user or group, if pass email adress // user | group | domain | $newpermission->settype('user'); //could owner can not set transferownership // organizer | owner | reader | writer $newpermission->setrole('writer'); $service->permissions->create($file->getid(), $newpermission); $file_name = str_replace('.docx', '.pdf', '281e2399740c88957143507721bd0f25.docx'); $attempt = 1; do{ //wait 5000ms usleep(500000*$attempt); //try pdf file. $content = $service->files->export($file->getid(), 'application/pdf', array( 'alt' => 'media' )); //save fetched data. file_put_contents($file_name, $content->getbody()->getcontents()); if(filesize($file_name)) break; else $attempt++; }while(true);
Comments
Post a Comment