Python 为上传到谷歌硬盘的私人文件生成可下载链接

Python 为上传到谷歌硬盘的私人文件生成可下载链接,python,file,google-drive-api,drive,Python,File,Google Drive Api,Drive,是否可以为上传到谷歌硬盘的私人文件生成可下载链接 我尝试过使用FilesAPI并生成了“webContent链接”,但它只对所有者和共享用户开放 (需要可以与任何人共享的公共链接) 您想让任何人使用webContentLink下载该文件 您想使用驱动器API v2 您希望使用Python中的“请求”模块来实现这一点 您已经能够使用驱动器API上载和下载该文件 如果我的理解是正确的,这次修改怎么样 修改点: 为了让任何人使用webContentLink下载该文件,需要公开共享该文件。 在这

是否可以为上传到谷歌硬盘的私人文件生成可下载链接

我尝试过使用FilesAPI并生成了“webContent链接”,但它只对所有者和共享用户开放

(需要可以与任何人共享的公共链接)

  • 您想让任何人使用
    webContentLink
    下载该文件
  • 您想使用驱动器API v2
  • 您希望使用Python中的“请求”模块来实现这一点
  • 您已经能够使用驱动器API上载和下载该文件
如果我的理解是正确的,这次修改怎么样

修改点:
  • 为了让任何人使用
    webContentLink
    下载该文件,需要公开共享该文件。
    • 在这个修改后的脚本中,该文件以
      {'role':'reader','type':'any','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
    到范围
参考:

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

如果我们通过更新权限来做到这一点,驱动器中的文件会成为公共文件吗?@kkr感谢您的回复。在上面修改的脚本中,只有文件ID的文件是公开共享的。这样,任何人都可以以只读方式打开该文件。而且,只有知道URL的用户才能访问它,在这种情况下,通用搜索引擎无法搜索该文件。感谢您的回复和详细说明@tanaike@kkr欢迎如果我提议的脚本对你的目标没有用处,我道歉。不,这是我所期望的。谢谢你的帮助。
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