https://developers.google.com/drive/publish-siteで提供されているサンプルコードを使用して、GoogleドライブでWebアクセス可能なフォルダを作成する新しい機能を試しています。ただし、フォルダーが作成されるコードを実行すると、アクセス許可が設定されず、次のメッセージが表示されるため、Webリンクが返されません。
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()エラーを修正する方法を知っている人はいますか?