为 Google 驱动器中上传的私有文件生成可下载的 link

Generating a downloadable link for a private file uploaded in the Google drive

是否可以为上传到 google 驱动器的私有文件生成可下载的 link?

我已经尝试使用文件 API 并生成了 'webContent Link',但只有所有者和共享用户可以访问它。

(期待 public link 可以与任何人分享)

def file_info_drive(access_token, file_id):
    headers = {'Authorization': 'Bearer ' + access_token, "content-type": "application/json"}
    response = requests.get('https://www.googleapis.com/drive/v2/files/{file_id}', headers=headers)
    response = response.json()

    link = response['webContentLink']
    return link
  • 您想让任何人使用 webContentLink 下载文件。
  • 您想使用云端硬盘 API v2.
  • 您想使用带有 Python 的 'request' 模块来实现此目的。
  • 您已经可以使用云端硬盘上传和下载文件 API。

如果我的理解是正确的,这个修改怎么样?

修改点:

  • 为了让任何人使用 webContentLink 下载该文件,需要公开共享该文件。
    • 在此修改后的脚本中,文件以 {'role': 'reader', 'type': 'anyone', 'withLink': True} 的条件公开共享。在这种情况下,知道 URL 的人可以下载该文件。

修改后的脚本:

当你的脚本修改后,变成如下。

def file_info_drive(access_token, file_id):
    headers = {'Authorization': 'Bearer ' + access_token, "content-type": "application/json"}

    # Using the following script, the file is shared publicly. By this, anyone can download the file.
    payload = {'role': 'reader', 'type': 'anyone', 'withLink': True}
    requests.post('https://www.googleapis.com/drive/v2/files/{file_id}/permissions', json=payload, headers=headers)

    response = requests.get('https://www.googleapis.com/drive/v2/files/{file_id}', headers=headers)
    response = response.json()

    link = response['webContentLink']
    return link

注:

  • 在这种情况下,使用POST方法。因此,如果范围发生错误,请将 https://www.googleapis.com/auth/drive 添加到范围。

参考:

如果我误解了你的问题,这不是你想要的方向,我很抱歉。