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

↘锁芯ラ 提交于 2021-02-08 07:26:23

问题


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

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

回答1:


  • You want to make anyone download the file using webContentLink.
  • You want to use Drive API v2.
  • You want to achieve this using 'request' module with Python.
  • You have already been able to upload and download the file using Drive API.

If my understanding is correct, how about this modification?

Modification points:

  • 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.

Modified script:

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

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.

Reference:

  • Permissions: insert

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



来源:https://stackoverflow.com/questions/58318228/generating-a-downloadable-link-for-a-private-file-uploaded-in-the-google-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!