簡體   English   中英

Google_Service_Drive RetrieveListOfAllFiles僅返回一個文件

[英]Google_Service_Drive retrieveListOfAllFiles only returning one file

當我從php運行API時,返回的唯一項是該文件夾中沒有的“如何開始使用Drive”文件,但是當我從API網頁運行retrieveListOfAllFiles()時,我看到了驅動器中的所有文件。 這是代碼。

set_include_path(ABSPATH . "path-to/api/google-api-php-client/src/");
require_once "Google/Client.php";
require_once "Google/Service/Drive.php";
require_once "Google/Auth/OAuth2.php";
require_once "Google/Auth/AssertionCredentials.php";

  define('CLIENT_ID', 'xxxxx');
  define('SERVICE_ACCOUNT_NAME', 'xxxxx');
  $key    = file_get_contents(ABSPATH . "path-to-.p12");
  $scopes = array('https://www.googleapis.com/auth/drive.readonly');


  $client = new Google_Client();
  $client->setApplicationName("xxxx");

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

  $auth = new Google_Auth_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    $scopes,
    $key
  );

  $client->setAssertionCredentials($auth);

  if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($auth);
  }

  $_SESSION['service_token'] = $client->getAccessToken();

  $service = new Google_Service_Drive($client);

  $files = retrieveAllFiles($service);

文件僅返回“如何開始使用驅動器”文件-該頁面上的API測試如下: https : //developers.google.com/drive/v2/reference/files/list返回驅動器中的所有文件。

Ids是正確的,.p12文件是新的並且正在加載,我不知道出了什么問題。

我還注意到,如果我var_dump()的輸出

dump($service->files->listFiles(array()));

我也只得到一個“如何開始使用驅動器”文件。 如此看來,即使我沒有收到任何OAuth2錯誤,也沒有獲得密鑰令牌等,似乎仍無法訪問我要從中列出文件的驅動器。

問題已解決。 雲端硬盤中的文件夾必須與API用戶共享-以@ developer.gserviceaccount.com結尾的電子郵件-我在文檔中的任何地方都看不到...也許我是盲目的。 但是,看來這些API文檔已經過時了。

暫無
暫無

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

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