如何从Google Drive REST api v3获取共享链接?

问题描述 投票:0回答:1

将文件上传到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
}

GoogleDrive中的文件确实已变为共享状态:shared file in GoogleDrive

但是响应json中没有共享链接,因此我查看了文档,在here中,您可以找到fields参数(请参见下面的ScreenShot):fields parameter

单击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参数如何工作,因为我认为webViewLinknamemimeType是正确的字段,它们都在here中描述,有人这样做吗? 我不会使用google-api-php-client,因为它的大小太大(> 20M)。

google-drive-api google-api-php-client php-curl
1个回答
0
投票
    您想使用php检索webViewLink
© www.soinside.com 2019 - 2024. All rights reserved.