我一直在尝试使用此处提供的示例代码在 Google Drive 中创建 Web 可访问文件夹的新功能:https ://developers.google.com/drive/publish-site 。但是,当我运行创建文件夹的代码时,未设置权限并且未返回网络链接,因为我收到以下消息:
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() 错误?