问题
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.
- In this modified script, the file is publicly shared with the condition of
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