使用示例PHP代码创建google驱动器文件夹时出错

时间:2012-11-30 09:12:19

标签: php google-drive-api

我一直在尝试使用此处提供的示例代码创建可访问网络的文件夹ingoogle驱动器的新功能:https://developers.google.com/drive/publish-site。但是,当我运行创建文件夹的代码时,未设置权限,并且不会返回weblink,因为我收到以下消息:

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

代码如下,所有这些都来自样本:

<?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;
}
?>

我确实打印出$ file的内容,但是没有webViewLink成员,所以我猜是必须为此设置权限?任何人都知道如何修复getId()错误?

1 个答案:

答案 0 :(得分:3)

这也发生在我身上 - 在config.php中将'use-objects'设置为true。为我修好了