簡體   English   中英

列出並上傳到Google Drive API

[英]List and upload to Google Drive API

我正在嘗試使用Google Drive API學習一個簡單的Web應用程序(PHP)。

  1. 顯示驅動器中的文件列表。
  2. 將新文件添加到驅動器。

代碼如下:

<?php
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';

$client = new Google_Client();
$client->setClientId(''); // Kept my id
$client->setClientSecret(''); //kept my key
$client->setRedirectUri($url);
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

 if (isset($_GET['code'])) {
 $_SESSION['accessToken'] = $client->authenticate($_GET['code']);
 header('location:'.$url);exit;
 } elseif (!isset($_SESSION['accessToken'])) {
 $client->authenticate();
 } 

到這里為止一切正常。 該應用程序通過身份驗證,然后重定向回我的應用程序。 現在,我嘗試顯示文件列表。

問題代碼:

 $service = new Google_DriveService($client);
 $result = array();
  $files = $service->files->listFiles();
  $result = array_merge($result, $files->getItems());
 print_r($result);

我收到此錯誤:

錯誤:C:\\ wamp \\ www \\ Sample \\ google-api-php中出現未捕獲的異常“ Google_ServiceException”,消息為“調用GET https://www.googleapis.com/drive/v2/files時出錯:(401)需要登錄” -client \\ src \\ io \\ Google_REST.php

有人可以幫忙糾正我嗎,我也很樂意分享簡單的代碼如何插入文檔。

在$ client-> authenticate();之后添加此行:$ client-> setAccessToken($ accessToken);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM