0
votes

I'm trying to list files in Google Drive using Google's PHP API (https://developers.google.com/drive/v2/web/quickstart/php).

I've managed to obtain a scoped access token, but the sample code Google provides for listing files (https://developers.google.com/drive/v2/reference/files/list#response) is returning an error.

Here is my code:

$client = new Google_Client();
    $client->setAuthConfigFile('/usr/share/nginx/google.json');
    $client->setAccessToken($a4e->user->google_token); // access token scoped for DRIVE_READONLY
    $drive_service = new Google_Service_Drive($client);

    $files=retrieveAllFiles($drive_service);
    var_dump($files);

    function retrieveAllFiles($service) {
      $result = array();
      $pageToken = NULL;

      do {
        try {
          $parameters = array();
          if ($pageToken) {
            $parameters['pageToken'] = $pageToken;
          }
          $files = $service->files->listFiles($parameters);

          $result = array_merge($result, $files->getItems());
          $pageToken = $files->getNextPageToken();
        } catch (Exception $e) {
          print "An error occurred: " . $e->getMessage();
          $pageToken = NULL;
        }
      } while ($pageToken);
      return $result;
    }

This returns the following error:

Fatal error: Call to undefined method Google_Service_Drive_FileList::getItems() in /usr/share/nginx/html/tools/google/index.php on line 30

Does anyone know what I'm doing wrong?

Many thanks in advance.

1

1 Answers

2
votes

I guess you are using Drive API V3 and trying to invoke a method of version V2 in V3 . Clearly look in the documentation , the Drive API V2 returns response in following format { "kind": "drive#fileList", "etag": etag, "selfLink": string, "nextPageToken": string, "nextLink": string, "items": [ files Resource ] } So you can do a $files->getItems() in V2 . But the Drive API V3 return response for listing in following format { "kind": "drive#fileList", "nextPageToken": string, "files": [ files Resource ] } So , you may need to do a $files->getFiles(). Give it a try !! I hope this will work . For your reference Drive API V3 Listing Response Drive API V2 Listing Response . Let me know if issue is resolved .