【问题标题】:How can I get share link from Google Drive REST api v3?如何从 Google Drive REST api v3 获取共享链接?
【发布时间】:2020-07-02 23:03:25
【问题描述】:

将文件上传到 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 中的文件确实变成了共享状态:

但是响应json中没有分享链接,所以我查看了文档,在here,你可以找到fields参数(见下面的截图):

单击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 提供的 api 始终是一个不错的选择,RAW api 确实有效,但从编码和异常处理的角度来看都需要付出很多努力。我不确定为什么大于 20M 的大小对您来说是个问题,因为现在存储并不重要,除非您在物联网设备上运行服务器。
  • @SariqShaikh 其实这不是关于存储,而是关于 google-api-php-client 占用了我工具大小的 1/3(我正在开发一个上传工具),因为图书馆有很多问题,例如this one,并且它缺乏文档,就像我真的不知道如何使用google-api-php-client 上传多个块中的文件,有时文档没有php 示例,例如@ 987654328@,但我用 GuzzleHttp 解决了所有问题。

标签: google-drive-api google-api-php-client php-curl


【解决方案1】:
  • 您想使用 php 检索 webViewLink
  • 您想使用 curl 而不使用 php 的 googleapis 来实现此目的。
  • 您已经使用过 Drive API。

如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

问题:

  • 在您的情况下,您正尝试从 Permissions: create in Drive API 的方法中检索 webViewLink。不幸的是,Permissions: create 方法没有file 的字段。这样webViewLinknamemimeType的字段就不能直接从Permissions:create的方法中获取了。
  • 关于I really don't know how can this fields parameter works, cause I think webViewLink, name and mimeType are correct fields, they all describe in [here](https://developers.google.com/drive/api/v3/reference/files#resource-representations), anyone did this before?,
    • 在这种情况下,请检查Permissions。您可以在那里看到字段参数。

根据上述情况,会出现这样的错误。

解决方法 1:

幸运的是,webViewLink 的格式是常量。使用它,您可以使用下面的文件 ID 直接检索webViewLink

$webViewLink = 'https://drive.google.com/file/d/'.$fileId.'/view?usp=drivesdk';

解决方法 2:

如果你想使用 API 检索webViewLink,那么使用 Files: get in Drive API 的方法怎么样?示例脚本如下。

示例脚本:
function getWebViewLink($fileId, $accessToken){
    $ch = curl_init();
    $options = [
        CURLOPT_URL =>  'https://www.googleapis.com/drive/v3/files/'.$fileId.'?fields=webViewLink',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => ['Authorization:Bearer '.$accessToken],
    ];
    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);
    return $result;
}

参考:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

【讨论】:

  • 非常感谢您的回答,看完您的回答,我现在明白为什么webViewLinknamemimeType无法从Permissions的方法中检索到:create而且我认为解决方法1是最好的解决方案,解决方法2将需要一个api请求,这意味着需要更多时间来获取webViewLink,所以我将使用解决方法1,再次感谢您,非常感谢您。
  • 对了,你知道如何获取webViewLink 真实图片url,使用这个域“lh3.googleusercontent.com”的那个,你可以看看在 HTML 元素中,因为只有这个 url 可以嵌入到 markdown 文档中。
  • @Bruce Xie 感谢您的回复。我很高兴你的问题得到了解决。不幸的是,关于你的新问题,我无法理解。这是因为我的技术不好。我对此深表歉意。例如,将其发布为新问题怎么样?这样,很多用户可以看到您的问题,这将导致您的新问题的解决方案。非常抱歉,我无法尽快解决您的新问题。
  • 没关系,我会发一个新问题,再次感谢您的回答,真的很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-07
  • 2016-07-31
相关资源
最近更新 更多