duanlaican1849 2017-07-28 13:42
浏览 50
已采纳

Google Api for PHP(Drive API)导出为.pdf上传的.docx文件

I cannot obtain stable script when I try upload docx file to Google Drive, and then download that file but as 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 but in many cases request return normal response (200) but with Content-Length 0.

Am I doing something wrong? Or should I do some kind of loop, that try download file until success?

  • 写回答

1条回答 默认 最新

  • douling1936 2017-07-31 12:05
    关注

    OK. So I spend two days looking for solution and I came up with several conclusions:

    1. Using Server-to-Server authentication, you must create service account, with is "separate" google drive account. That's means that if you create file via script, file is created on service account, and you can access this file only via API.
    2. You can assign permissions to created file, that connect your real google account to file, and than you can access that file via Google Drive. You can not transfer ownership to file from service account to google account, because Google PHP API at this moment do not have proper method. Or I do not see way to setup this. Class summary.

    3. Key is to 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, that will have access to file.
    $newPermission->setEmailAddress('<email here>');
    
    //Must be user or group, if you pass email adress
    // user | group | domain | anyone
    $newPermission->setType('user');
    
    //Could be owner but I 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 to get pdf file.
      $content = $service->files->export($file->getId(), 'application/pdf', array( 'alt' => 'media' ));
    
      //Save just fetched data.
      file_put_contents($file_name, $content->getBody()->getContents());
    
      if(filesize($file_name)) break;
      else $attempt++;
    
    }while(true);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响
  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题