我尝试上传文件并将其分享给公众。文件已上传,但我无法设置权限。这是我的代码:
<?php
....
$client = new Google_Client();
$client->setClientId(GOOGLE_ID);
$client->setClientSecret(GOOGLE_SECRET);
$client->setAccessType("offline");
$client->setScopes(array( //just to be sure included all scopes
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive.readonly.metadata",
"https://docs.google.com/feeds",
"https://www.googleapis.com/auth/drive.apps.readonly",
"https://www.googleapis.com/auth/drive.readonly",
"https://www.googleapis.com/auth/drive.install",
"https://www.googleapis.com/auth/drive.appdata",
"https://www.googleapis.com/auth/drive.scripts"
));
$service = new Google_Service_Drive($client);
...
//setting a token and successfully uploading a file
...
$perm = new Google_Service_Drive_Permission();
$perm->setType("anyone");
$perm->setRole("reader");
$perm->setValue("");
$perm->setWithLink(false);
try {
$a = $service->permissions->insert($file->getId(), $perm);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
它不会抛出任何异常,返回一个对象。
顺便说一下,这些是从这个文件收到的权限:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
修改
我在文档中发现必须发送权限值或ID。如果我说得对,那id必须是我的申请电子邮件吗?我试过$perm->setId(GOOGLE_ID);
但是没有改变
响应转储:
Google_Http_Request Object
(
[batchHeaders:Google_Http_Request:private] => Array
(
[Content-Type] => application/http
[Content-Transfer-Encoding] => binary
[MIME-Version] => 1.0
)
[queryParams:protected] => Array
(
)
[requestMethod:protected] => POST
[requestHeaders:protected] => Array
(
[content-type] => application/json; charset=UTF-8
[authorization] => Bearer xxxxxxxxxxxxxx
)
[baseComponent:protected] => https://www.googleapis.com
[path:protected] => /drive/v2/files/0B3_LVXxRuaPoT0xQYUZxbjlQTUE/permissions
[postBody:protected] => {"id":"254599272636-1o7pt59olp5f1v4k4epui1k73oqonrl9@developer.gserviceaccount.com","role":"reader","type":"anyone"} //its a test value, i send my id instead of me
[userAgent:protected] =>
[canGzip:protected] =>
[responseHttpCode:protected] =>
[responseHeaders:protected] =>
[responseBody:protected] =>
[expectedClass:protected] => Google_Service_Drive_Permission
[accessKey] =>
)
修改
我也尝试使用$permissionId = $service->permissions->getIdForEmail($email);
方法,但它也没有用,似乎没有任何与权限相关的工作。
答案 0 :(得分:1)
根据这里的答案How to create a public Google Doc through the Drive API (PHP client),您应该将'我'传递给setValue。
$permission = new Google_Permission();
$permission->setRole( 'writer' );
$permission->setType( 'anyone' );
$permission->setValue( 'me' );
$service->permissions->insert( $file->getId(), $permission );
答案使用writer
作为权限级别,但reader
也应该有效。
我发现您的代码和引用的答案之间存在另一个区别。使用Google_DriveService
Google_Service_Drive
$service = new Google_DriveService( $client );