简体   繁体   English

Google_Service_Drive RetrieveListOfAllFiles仅返回一个文件

[英]Google_Service_Drive retrieveListOfAllFiles only returning one file

When I run the API from php the only item returned is the "How to get started with Drive" file which isn't in the folder, but when I run the retrieveListOfAllFiles() from the API webpage I see all the files in the drive. 当我从php运行API时,返回的唯一项是该文件夹中没有的“如何开始使用Drive”文件,但是当我从API网页运行retrieveListOfAllFiles()时,我看到了驱动器中的所有文件。 Here is the code. 这是代码。

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);

Files only returns the "how to get started with drive" file - where as the API test from this page: https://developers.google.com/drive/v2/reference/files/list returns all the files in the drive. 文件仅返回“如何开始使用驱动器”文件-该页面上的API测试如下: https : //developers.google.com/drive/v2/reference/files/list返回驱动器中的所有文件。

The Ids are correct, the .p12 file is new and loading, I have no idea what is going wrong. Ids是正确的,.p12文件是新的并且正在加载,我不知道出了什么问题。

I have also noticed that if I var_dump() the output of this 我还注意到,如果我var_dump()的输出

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

I also only get the single "how to get started with drive" file. 我也只得到一个“如何开始使用驱动器”文件。 So it seems like even though I'm not getting any OAuth2 errors, and getting a key token, etc, the drive I want to list the files from is not being accessed. 如此看来,即使我没有收到任何OAuth2错误,也没有获得密钥令牌等,似乎仍无法访问我要从中列出文件的驱动器。

Problem resolved. 问题已解决。 A folder from the Drive must be shared with the API user - the email ending in @developer.gserviceaccount.com - which I didn't see in the docs anywhere... maybe I'm blind. 云端硬盘中的文件夹必须与API用户共享-以@ developer.gserviceaccount.com结尾的电子邮件-我在文档中的任何地方都看不到...也许我是盲目的。 But those API docs are pretty out of date, it seems. 但是,看来这些API文档已经过时了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM