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

Google Api for PHP(驱动Api)导出为.pdf上传的.docx文件,php,rest,google-api,google-drive-api,google-client,Php,Rest,Google Api,Google Drive Api,Google Client,我无法获得稳定的脚本时,我试图上传docx文件到谷歌驱动器,然后下载该文件,但作为PDF格式 代码: 此代码在..中工作。。20-30%的使用率。有时,$service->files->export()返回错误代码500,但在许多情况下,请求返回正常响应(200),但内容长度为0 我做错什么了吗?或者我应该做一些循环,尝试下载文件直到成功吗?好的。因此,我花了两天时间寻找解决方案,并得出了几个结论: 使用服务器到服务器身份验证,您必须创建服务帐户,并使用“独立”的google驱动器帐户。这意味着

我无法获得稳定的脚本时,我试图上传docx文件到谷歌驱动器,然后下载该文件,但作为PDF格式

代码:

此代码在..中工作。。20-30%的使用率。有时,$service->files->export()返回错误代码500,但在许多情况下,请求返回正常响应(200),但内容长度为0


我做错什么了吗?或者我应该做一些循环,尝试下载文件直到成功吗?

好的。因此,我花了两天时间寻找解决方案,并得出了几个结论:

  • 使用服务器到服务器身份验证,您必须创建服务帐户,并使用“独立”的google驱动器帐户。这意味着,若您通过脚本创建文件,那个么该文件将在服务帐户上创建,并且您只能通过API访问该文件
  • 您可以为创建的文件分配权限,将您的真实google帐户连接到该文件,然后您可以通过google Drive访问该文件。您不能将文件的所有权从服务帐户转移到google帐户,因为google PHP API目前没有合适的方法。或者我看不到设置这个的方法

  • 关键是使用。换句话说,除非成功,否则要努力。(°)

  • 代码:

    //谷歌API
    一次需要_('vendor/autoload.php');
    putenv('GOOGLE_APPLICATION_CREDENTIALS='。uuu DIR_uuu.'/2ab4ece19bd5.json');
    $client=新的Google_客户端();
    $client->setApplicationName('sp-gen');
    $client->setScopes(阵列)https://www.googleapis.com/auth/drive'));
    $client->useApplicationDefaultCredentials();
    $service=新的谷歌服务驱动器($client);
    $fileMetadata=新的Google\u服务\u驱动器\u驱动器文件(阵列)(
    “名称”=>“281E2399740C8895714350772BD0F25.docx”,
    'mimeType'=>'应用程序/vnd.google apps.document'
    ));
    $content=file_get_contents('281E2399740C8895714350772BD0F25.docx');
    $file=$service->files->create($fileMetadata,数组)(
    “数据”=>$content,
    “uploadType”=>“多部分”
    );
    //创建新权限
    $newPermission=新Google_服务_驱动器_权限();
    //设置有权访问文件的帐户的电子邮件。
    $newPermission->setEmailAddress(“”);
    //如果您传递电子邮件地址,则必须是用户或组
    //用户|组|域|任何人
    $newPermission->setType('user');
    //可以是所有者,但我无法设置所有权
    //组织者|所有者|读者|作者
    $newPermission->setRole('writer');
    $service->permissions->create($file->getId(),$newPermission);
    $file_name=str_replace('.docx','.pdf',281E2399740C8895714350772BD0F25.docx');
    $trunt=1;
    做{
    //等待5000毫秒
    美国LEEP(500000*次尝试);
    //尝试获取pdf文件。
    $content=$service->files->export($file->getId(),'application/pdf',数组('alt'=>'media');
    //保存刚获取的数据。
    文件内容($file\u name,$content->getBody()->getContents());
    if(filesize($file_name))中断;
    else$trust++;
    }虽然(正确);
    
    $file->id
    没有这样的键吗?不应该是
    $file->data
    $service->files->create()
    return object with id field包含在Google Drive上创建的文件的id。错误代码500除外
    $file->id
    始终存在。创建文件和导出callI之间可能存在延迟。我尝试了1-3秒延迟,但没有发现任何改进。可能延迟太小。我会检查。您的退避是线性的,而不是指数型的。
    //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());
    
    //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);