Search code examples
phpgoogle-drive-api

Undefined property: Google_Service_Books::$files


I am trying to get a list of files from my Google Drive from a local PHP app, here is what I have:

$client = new Google_Client();
$client->setApplicationName("My Application");
$client->setDeveloperKey("AIzaSyAmWcZArf7NCk4d65HoKZzLENCJ6cx9fNg");
$service = new Google_Service_Books($client);


 /**
 * Retrieve a list of File resources.
 *
 * @param Google_Service_Drive $service Drive API service instance.
 * @return Array List of Google_Service_Drive_DriveFile resources.
 */
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;
}


retrieveAllFiles($service); 

Taken straight from The docs.

I get the following errors when I run this:

enter image description here

Anyone know why this is happening?


Solution

  • Shouldn't you use Google_Service_Drive instead of Google_Service_Books?

    Google_Service_Drive:

    class Google_Service_Drive extends Google_Service
    {
        /** View and manage the files and documents in your Google Drive. */
        const DRIVE = "https://www.googleapis.com/auth/drive";
        /** View and manage its own configuration data in your Google Drive. */
        const DRIVE_APPDATA = "https://www.googleapis.com/auth/drive.appdata";
        /** View your Google Drive apps. */
        const DRIVE_APPS_READONLY = "https://www.googleapis.com/auth/drive.apps.readonly";
        /** View and manage Google Drive files that you have opened or created with this app. */
        const DRIVE_FILE = "https://www.googleapis.com/auth/drive.file";
        /** View metadata for files and documents in your Google Drive. */
        const DRIVE_METADATA_READONLY = "https://www.googleapis.com/auth/drive.metadata.readonly";
        /** View the files and documents in your Google Drive. */
        const DRIVE_READONLY = "https://www.googleapis.com/auth/drive.readonly";
        /** Modify your Google Apps Script scripts' behavior. */
        const DRIVE_SCRIPTS = "https://www.googleapis.com/auth/drive.scripts";
        public $about;
        public $apps;
        public $changes;
        public $channels;
        public $children;
        public $comments;
        public $files;
        public $parents;
        public $permissions;
        public $properties;
        public $realtime;
        public $replies;
        public $revisions;
    

    Google_Service_Books:

    class Google_Service_Books extends Google_Service
    {
        /** Manage your books. */
        const BOOKS = "https://www.googleapis.com/auth/books";
        public $bookshelves;
        public $bookshelves_volumes;
        public $cloudloading;
        public $layers;
        public $layers_annotationData;
        public $layers_volumeAnnotations;
        public $myconfig;
        public $mylibrary_annotations;
        public $mylibrary_bookshelves;
        public $mylibrary_bookshelves_volumes;
        public $mylibrary_readingpositions;
        public $promooffer;
        public $volumes;
        public $volumes_associated;
        public $volumes_mybooks;
        public $volumes_recommended;
        public $volumes_useruploaded;