将文件上传到GoogleDrive之后,我可以从响应中获得fileId
,然后将fileId
传递给以下函数:
public function createShareLink($fileId, $accessToken){
$ch = curl_init();
$options = [
CURLOPT_URL => 'https://www.googleapis.com/drive/v3/files/'.$fileId.'/permissions',
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode(['type'=>'anyone', 'role'=>'reader',]),
CURLOPT_HTTPHEADER => [
'Authorization:Bearer '.$accessToken,
'Content-Type:application/json',
],
//In case you're in Windows, sometimes will throw error if not set SSL verification to false
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
];
//In case you need a proxy
//$options[CURLOPT_PROXY] = 'http://127.0.0.1:1087';
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
return $result;
}
它确实起作用并且它返回一个json:
{
"kind": "drive#permission",
"id": "anyoneWithLink",
"type": "anyone",
"role": "reader",
"allowFileDiscovery": false
}
但是响应json中没有共享链接,因此我查看了文档,在here中,您可以找到fields
参数(请参见下面的ScreenShot):
单击partial response会将您重定向到一个页面,其中包含一些有关如何将值传递给fileds
参数的示例。
我按照示例将webViewLink
作为值传递给fields
,如下所示:
CURLOPT_URL => 'https://www.googleapis.com/drive/v3/files/'.$fileId.'/permissions?fields=webViewLink',
但请回答错误:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalidParameter",
"message": "Invalid field selection webViewLink",
"locationType": "parameter",
"location": "fields"
}
],
"code": 400,
"message": "Invalid field selection webViewLink"
}
}
我尝试过id
:
CURLOPT_URL => 'https://www.googleapis.com/drive/v3/files/'.$fileId.'/permissions?fields=id',
响应为:
{
"id": "anyoneWithLink"
}
我尝试过name
:
CURLOPT_URL => 'https://www.googleapis.com/drive/v3/files/'.$fileId.'/permissions?fields=name',
响应为:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalidParameter",
"message": "Invalid field selection name",
"locationType": "parameter",
"location": "fields"
}
],
"code": 400,
"message": "Invalid field selection name"
}
}
我尝试过mimeType
:
CURLOPT_URL => 'https://www.googleapis.com/drive/v3/files/'.$fileId.'/permissions?fields=mimeType',
响应为:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalidParameter",
"message": "Invalid field selection mimeType",
"locationType": "parameter",
"location": "fields"
}
],
"code": 400,
"message": "Invalid field selection mimeType"
}
}
我真的不知道此fields
参数如何工作,因为我认为webViewLink
,name
和mimeType
是正确的字段,它们都在here中描述,有人这样做吗? 我不会使用google-api-php-client,因为它的大小太大(> 20M)。
webViewLink
。