为在Google云端硬盘中上传的私人文件生成可下载的链接 [英] Generating a downloadable link for a private file uploaded in the Google drive

查看:127
本文介绍了为在Google云端硬盘中上传的私人文件生成可下载的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为在Google驱动器中上传的私人文件生成可下载的链接?

Is it possible to generate the downloadable link for a private file uploaded in the google drive?

我尝试使用文件API并生成了"webContent链接",但是只有所有者和共享用户才能访问它.

I have tried with the files API and generated the 'webContent Link', but it is accessible only to the owner and the shared users.

(期望可以与任何人共享的公共链接)

(Expecting a public link that can be shared with anyone)

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 下载文件.
  • 您要使用Drive API v2.
  • 您想通过Python的'request'模块实现这一目标.
  • 您已经能够使用Drive API上传和下载文件.
  • 如果我的理解是正确的,那么该修改如何?

    If my understanding is correct, how about this modification?

    • 为了使任何人都可以使用 webContentLink 下载文件,需要公开共享文件.
      • 在此修改后的脚本中,文件以 {'role':'reader','type':'anyone','withLink':True} 的条件公开共享.在这种情况下,知道URL的人可以下载文件.
      • In order to make anyone download the file using webContentLink, it is required to share publicly the file.
        • In this modified script, the file is publicly shared with the condition of {'role': 'reader', 'type': 'anyone', 'withLink': True}. In this case, the persons who know the URL can download the file.

        修改脚本后,它如下所示.

        When your script is modified, it becomes as follows.

        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 添加到作用域.
        • Note:

          • In this case, the POST method is used. So if an error for the scopes occurs, please add https://www.googleapis.com/auth/drive to the scope.
          • 如果我误解了您的问题,而这不是您想要的方向,我深表歉意.

            If I misunderstood your question and this was not the direction you want, I apologize.

            这篇关于为在Google云端硬盘中上传的私人文件生成可下载的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆