简体   繁体   中英

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. 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.

The Ids are correct, the .p12 file is new and loading, I have no idea what is going wrong.

I have also noticed that if I var_dump() the output of this

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.

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. But those API docs are pretty out of date, it seems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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