douchenchepan6465 2016-10-09 20:50
浏览 33

php服务器谷歌驱动器下载文件在后台

I need form PHP level, in background without user interaction login on google drive and get files list. I found similar topic and code for question working, but is required handly login, where I need login from php in background.

In second post with code for possibly login witout user interaction, but I don't know where is bug. My code PHP:

<?php  
require_once 'vendor/autoload.php';
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_DriveService.php';
require_once 'src/auth/Google_AssertionCredentials.php';


$client_email = 'xxxxxx@xxxxxxxxxxxxx.xxx.gserviceaccount.com';
$private_key = file_get_contents('key.p12');
$user_to_impersonate = 'xxxxxxxxxxxxxxx@gmail.com';

$scopes = array('https://www.googleapis.com/auth/drive');
$credentials = new Google_AssertionCredentials(
        $client_email,
        $scopes,
        $private_key,
        'notasecret',  // Default P12 password
        'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type
        $user_to_impersonate
    );

 $client = new Google_Client();
 $client->setAssertionCredentials($credentials);
 if ($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion();
 }

 $service = new Google_Service_Drive($client);

 $files = $service->files->listFiles();
 echo "count files=".count($files)."<br>";       

 foreach( $files as $item ) {
     echo "title=".$item['title']."<br>";
 }
 ?>

Vendor directory I get from this instruction, and other files I get from this GitHub

I had problem with function Google_Auth_AssertionCredentials, PHP hasn't file with this function. I found that file src/auth/Google_AssertionCredentials.php has similar function Google_AssertionCredentials. I included Google_AssertionCredentials.php file and changed function name.

In finally I have new error:

PHP Fatal error: Cannot call constructor in \google_drive\vendor\google\apiclient-services\src\Google\Service\Drive.php on line 75

I don't know what doing again. I tried many other metod for login on google drive, eg. load file list GET method with API_KEY, or via json file.

As result I want get file list, download them. edit and upload.

Any sugestions?

EDIT:

I have part sucess. I found liblary this, and them work with this code:

<?php  
function retrieveAllFiles($service) {
  $result = array();
  $pageToken = NULL;

  do {
    try {
      $parameters = array();
      if ($pageToken) {
        $parameters['pageToken'] = $pageToken;
      }
      $files = $service->files->listFiles($parameters);

      $result = array_merge($result, $files->getItems());
      $pageToken = $files->getNextPageToken();
    } catch (Exception $e) {
      print "An error occurred: " . $e->getMessage();
      $pageToken = NULL;
    }
  } while ($pageToken);
  return $result;
}

session_start();
require_once '/google-api-php-client/autoload.php';


$client_email = 'xxxx@xxxxx.iam.gserviceaccount.com';
$user_to_impersonate = 'xxxxxx@gmail.com';
$private_key = file_get_contents('key.p12');

$scopes = array('https://www.googleapis.com/auth/drive');
$credentials = new Google_Auth_AssertionCredentials(
        $client_email,
        $scopes,
        $private_key,
        'notasecret',  // Default P12 password
        'http://oauth.net/grant_type/jwt/1.0/bearer',
        $user_to_impersonate
    );

 $client = new Google_Client();
 if(isset($_SESSION['service_token'])==false && $_SESSION['service_token']=='') {
     $client->setAssertionCredentials($credentials);
     if ($client->getAuth()->isAccessTokenExpired()) {
            $client->getAuth()->refreshTokenWithAssertion($credentials);
     }
    $_SESSION['service_token'] = $client->getAccessToken();
}

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

        if ($client->isAccessTokenExpired()) {
            $client->refreshToken($client->getRefreshToken());
            $_SESSION['service_token'] =  $client->getAccessToken();
        }

$service = new Google_Service_Drive($client);
print_r(retrieveAllFiles($service));
}
 ?>

And in result I have only one file [originalFilename] => Getting started, this is PDF, but I don't see this file on Gogle dirve. On google drive I uploaded file: README.md.

I'm not sure, but maybe this file is on ` 'xxxx@xxxxx.iam.gserviceaccount.com' and script not logged to 'xxxxxx@gmail.com'?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用