williamsdb
williamsdb

Reputation: 1064

Error when creating google drive folder using sample PHP code

I have been trying out the new ability to create a web accessible folder ingoogle drive using the sample code provided here: https://developers.google.com/drive/publish-site. However, when I run the code which the folder gets created the permissions are not set and the weblink is not returned as I get the following message:

PHP Fatal error:  Call to a member function getId() on a non-object in /var/www/gdrive/folder.php on line 43 

The code is as follows all of which comes from the samples:

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

$client = new Google_Client();
// Get your credentials from the APIs Console
$client->setClientId('my client id');
$client->setClientSecret('my secret key');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

$service = new Google_DriveService($client);

$authUrl = $client->createAuthUrl();

//Request authorization
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));

// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);

$folder = createPublicFolder($service, "webtest");

print_r $folder;

function createPublicFolder($service, $folderName) {
  $file = new Google_DriveFile();
  $file->setTitle($folderName);
  $file->setMimeType('application/vnd.google-apps.folder');

  $createdFile = $service->files->insert($file, array(
      'mimeType' => 'application/vnd.google-apps.folder',
  ));

  $permission = new Google_Permission();
  $permission->setValue('');
  $permission->setType('anyone');
  $permission->setRole('reader');

 $service->permissions->insert($createdFile->getId(), $permission);

  return $file;
}
?>

I did print out the contents of $file but that doesn't have a webViewLink member so I am guessing that the permissions have to be set for this? Anyone know how to fix the getId() error?

Upvotes: 0

Views: 1261

Answers (1)

scott19_68
scott19_68

Reputation: 63

This happened to me too - set 'use-objects' to true in config.php. Fixed it for me

Upvotes: 3

Related Questions