doupeng2253 2013-11-19 14:38
浏览 53
已采纳

Google Drive API

I am trying to integrate Google Drive in my webpage by using the GoogleDrive API. I have copied most of the code from google.And when executing it, I have an issue with the function given. This is my code:

    $apiConfig['use_objects'] = true;
session_start();
$url_array = explode('?', 'http://'.$_SERVER ['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$url = $url_array[0];
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
require_once 'google-api-php-client/src/service/Google_Service.php';
require_once 'google-api-php-client/src/service/Google_ServiceResource.php';


$client = new Google_Client();
$client->setClientId('347297625177-l6476q96da6fi65iqp0ra84e3luic5kp.apps.googleusercontent.com');
$client->setClientSecret('8UE-faGCj9xBKe8IsgP0pN2r');
$client->setRedirectUri("http://arodrisa.url.ph/GoogleDrive_example/list.php");
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

echo "5";
if (isset($_GET['code'])) {
    $_SESSION['accessToken'] = $client->authenticate($_GET['code']);
    header('location:'.$url);exit;
} elseif (!isset($_SESSION['accessToken'])) {
    $client->authenticate();
}
echo"2";
$result = array();
$pageToken = NULL;



do {
  try {
    $parameters = array();
    if ($pageToken) {
      $parameters['pageToken'] = $pageToken;
    }
    $files = $service->files->listFiles($parameters);
    $list=$files['items'];
    $result = array_merge($result,$list);
    $pageToken = $files['nextPageToken'];
  } catch (Exception $e) {
    echo "An error occurred: " . $e->getMessage();
    $pageToken = NULL;
  }
} while ($pageToken);
    var_dump($result);
    include 'list.html';

I get this error:

52 Fatal error: Call to a member function listFiles() on a non-object in /home/u317564784/public_html/GoogleDrive_example/list.php on line 39

Which referes to this line:

 $files = $service->files->listFiles($parameters);

Please help me.

  • 写回答

1条回答 默认 最新

  • duankeng2026 2017-07-20 09:26
    关注

    SOLVED I have added these lines

       $client->setAccessToken($_SESSION['accessToken']); 
       $service = new Google_DriveService($client); 
       $finfo = finfo_open(FILEINFO_MIME_TYPE); 
       $file = new Google_DriveFile();
    

    before

       $files = $service->files->listFiles($parameters);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大