Php 谷歌驱动器API-下载文件

Php 谷歌驱动器API-下载文件,php,google-drive-api,Php,Google Drive Api,使用oauth2连接,我有一个工作脚本(列出来自Google Drive的文档),我只需要添加一个下载/导出功能 注意:已设置访问令牌/刷新令牌(不同脚本) //错误报告(E\u ALL); 一次需要_('vendor/autoload.php'); 只需要一次('vendor/google/apiclient/src/google/Client.php'); require_once('vendor/google/apiclient services/src/google/Service/Dr

使用oauth2连接,我有一个工作脚本(列出来自Google Drive的文档),我只需要添加一个下载/导出功能

注意:已设置访问令牌/刷新令牌(不同脚本)

//错误报告(E\u ALL);
一次需要_('vendor/autoload.php');
只需要一次('vendor/google/apiclient/src/google/Client.php');
require_once('vendor/google/apiclient services/src/google/Service/Drive.php');
会话_start();
$client_id='';
$client_secret='';
$redirect_uri='';
$client=新的Google_客户端();
$client->setApplicationName(“谷歌日历”);
$client->setClientId($client\u id);
$client->setClientSecret($client\u secret);
$client->setRedirectUri($redirect\u uri);
$client->setAccessType('offline');//给我们拿我们的令牌
$client->addScope(“https://www.googleapis.com/auth/drive");
##步骤1:设置令牌##
$array=array(
'访问令牌'=>'XXXXXXXXXXXX',//设置访问令牌
'令牌类型'=>'承载',
'expires_in'=>0,
“已创建”=>0,
'刷新令牌'=>'XXXXXXXXXXXX',//设置刷新令牌
);
$\会话['token']=$array;
##步骤2:如果令牌处于活动状态,则列出文档##
如果(isset($\u会话['token'])){
$client->setAccessToken($_会话['token']);
//获取API客户端并构造服务对象。
$service=新的谷歌服务驱动器($client);
//打印最多10个文件的名称和ID。
$optParams=阵列(
“页面大小”=>10,
'字段'=>'下一个GetOken,文件(id,名称)'
);
$results=$service->files->listFiles($optrams);
如果(计数($results->getFiles())==0){
打印“找不到文件。\n”;
}否则{
打印“文件:\n”;
foreach($results->getFiles()作为$file){
printf(“%s(%s)\n
”,$file->getName(),$file->getId()); } } }
不幸的是,我无法创建直接下载链接(非公共文件)。我找到了以下文档,但我不知道如何应用这些文档:

“管理下载”中的PHP示例导致错误:

未定义变量:中的driveService。。。my_drive_script.php

只需添加链接即可,但仅适用于共享文档(或者如果我是在同一用户下登录的)

$fileid=$file->getId();
printf(“”);
我使用了来自的链接格式


应该可以下载非公开文件,对吗?

嘿,史蒂夫,删除了我以前的答案,因为你添加了更多信息,所以没有必要这么做。如果我能找到问题的根源并帮你解决,我会报告的,我祝你解决这个问题好运谢谢tcsh,我再次更新了我的帖子,感觉解决方案越来越接近:)嘿,史蒂夫,删除了我以前的答案,因为你添加了进一步的信息,所以没有必要。如果我能找到问题的根源并帮助你解决问题,我会向你报告,祝你解决问题好运谢谢tcsh,我再次更新了我的帖子,感觉解决方案越来越接近:)
//error_reporting(E_ALL);
require_once('vendor/autoload.php');
require_once('vendor/google/apiclient/src/Google/Client.php');
require_once('vendor/google/apiclient-services/src/Google/Service/Drive.php');

session_start();

$client_id = '<client_id>';
$client_secret = '<client_secret>';
$redirect_uri = '<redirect_uri>';

$client = new Google_Client();
$client->setApplicationName("Google Calendar");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setAccessType('offline'); // Gets us our refreshtoken
$client->addScope("https://www.googleapis.com/auth/drive");

 ## Step 1: Set token ##

       $array = array(
           'access_token'    => 'xxxxxxxxxxxxx', //set access token
           'token_type'      => 'Bearer',
           'expires_in'      => 0,
           'created'         => 0,
           'refresh_token'   => 'xxxxxxxxxxxxx', //set refresh token
       );

       $_SESSION['token'] = $array;

    ## Step 2: If token is active, list documents ##

    if (isset($_SESSION['token'])) {
        $client->setAccessToken($_SESSION['token']);

          // Get the API client and construct the service object.

            $service = new Google_Service_Drive($client);

              // Print the names and IDs for up to 10 files.
              $optParams = array(
                'pageSize' => 10,
                'fields' => 'nextPageToken, files(id, name)'
              );
              $results = $service->files->listFiles($optParams);

              if (count($results->getFiles()) == 0) {
                print "No files found.\n";
              } else {
                print "Files:\n";
                foreach ($results->getFiles() as $file) {
                  printf("%s (%s)\n<br>", $file->getName(), $file->getId());

                }
              }

      }
$fileid = $file->getId();
printf("<a href='https://docs.google.com/document/d/$fileid/export?format=doc'>Download doc</a>");