使用示例PHP代码创建谷歌驱动器文件夹时出错 [英] Error when creating google drive folder using sample PHP code
问题描述
我一直在尝试使用此处提供的示例代码创建可访问网络文件夹的ingoogle驱动器的新功能: https://developers.google.com/drive/publish-site 。但是,当我运行创建文件夹的代码时,未设置权限,并且网络链接不会返回,因为我收到以下消息:
PHP致命错误:在第43行的/var/www/gdrive/folder.php中的非对象上调用成员函数getId()。
代码如下,全部来自样本:
<?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();
//从API控制台获取您的凭证
$ 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();
//请求授权
打印请访问:\ $ $ authUrl\\\
\\\
;
print请输入验证码:\\\
;
$ authCode = trim(fgets(STDIN));
//访问令牌的交换授权码
$ accessToken = $ client-> authenticate($ authCode);
$ client-> setAccessToken($ accessToken);
$ folder = createPublicFolder($ service,webtest);
print_r $文件夹;
函数createPublicFolder($ service,$ folderName){
$ file = new Google_DriveFile();
$ file-> setTitle($ folderName);
$ file-> setMimeType('application / vnd.google-apps.folder');
$ b $ 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);
返回$ file;
}
?>
我打印了$文件的内容,但没有webViewLink成员,所以我猜测必须为此设置权限?任何人都知道如何解决getId()错误?
这也发生在我身上 - 将'use-objects'设置为true在config.php中。为我解决了这个问题
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?
This happened to me too - set 'use-objects' to true in config.php. Fixed it for me
这篇关于使用示例PHP代码创建谷歌驱动器文件夹时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!