1

I have managed to integrate the google Picker. Now I have the FileId and the access_token. I need to download the selected file in my back end which is in Python. I have followed the documentation on google developers but If i use the python library then i have to authenticate the user again which is not suitable in my case. Please enlighten me if I can download the file with any format.

Thanks in advance.

susheel
  • 93
  • 10
  • What mimeType do you want to download from Google Drive? If you want to download Google Docs as any format, you can see the information from https://developers.google.com/drive/v3/web/manage-downloads#downloading_google_documents In the case of files except for Google Docs, basically the original format can be downloaded. And then, you already have access token. So you can download the files using that and ``requests``. If I misunderstand your question, I'm sorry. – Tanaike Aug 21 '17 at 23:19
  • Thanks for information @Tanaike I want to download the user selected image and videos. I tried the `GET https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media Authorization: Bearer ` But its giving the file data. I dont have extension of the file. So is there any way that I can download the selected file by request with file id. I dont want to use the google python library. Please suggest. – susheel Aug 22 '17 at 05:43

1 Answers1

1

This is a very simple sample script for downloading files using access token and file id without Google library. And this sample supposes files (images and videos) except for Google Docs, as you said. The flow is as follows.

Flow :

  1. Retrieve filename and mimeType.
  2. Create filename from retrieved filename and mimeType. If the filename doesn't have the extension on Google Drive, this script adds the extension to the filename using mimeType and save it. If the filename has the extension on Google Drive, this script uses the original filename and save it.
  3. Download a file and save it as the created filename. If you want to save the file to the specific directory, please set it by yourself.

Sample script :

import mimetypes
import os.path
import requests
accessToken = "### access token ###"
fileId = "### file id ###"
fileInf = requests.get(
    "https://www.googleapis.com/drive/v3/files/" + fileId,
    headers={"Authorization": "Bearer " + accessToken},
)
filename = fileInf.json()["name"]
temp, ext = os.path.splitext(filename)
filename = filename if ext != "" else filename + mimetypes.guess_extension(fileInf.json()["mimeType"])
r = requests.get(
    "https://www.googleapis.com/drive/v3/files/" + fileId + "?alt=media",
    headers={"Authorization": "Bearer " + accessToken},
)
with open(filename, "wb") as f:
    f.write(r.content)

If this was not helpful for you, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thanks for the script @Tanaike. I think my `accessToken` is not valid. This accessToken is from the google picker. `fileInf` returning the `401` header with `{'location': 'Authorization', 'message': 'Invalid Credentials', 'domain': 'global', 'reason': 'authError', 'locationType': 'header'}`. Please suggest where can I get the access token if this is not correct? – susheel Aug 22 '17 at 09:04
  • @Tanaike is it possible to convert your script to use google drive api python library ('google-api-python-client')? – Anthony Kong Sep 04 '17 at 01:59
  • @Anthony Kong Can you submit it as a new question? – Tanaike Sep 04 '17 at 02:00
  • @Tanaike Absolutely! Just wanna check with you if it is feasible at all first – Anthony Kong Sep 04 '17 at 02:01
  • @Anthony Kong Yes. I think that you can achieve it using [this sample](https://developers.google.com/drive/v3/web/quickstart/python). – Tanaike Sep 04 '17 at 02:04
  • @Anthony Kong If you want a sample script, feel free to tell me. – Tanaike Sep 04 '17 at 02:08
  • @Tanaike Thanks but I think the sample is more for 'server-to-server' kind of authentication. Here is my question: https://stackoverflow.com/questions/45429618/unable-to-export-a-google-slide-as-pdf-format-on-server-side-likely-google-auth The key similarity between my question and this question is: picker api is executed in browser and the access token is passed to a python server (mine is running on an aws ec2 server) – Anthony Kong Sep 04 '17 at 02:09
  • @Anthony Kong Can I ask you about some question? 1. How are you managing the refresh token? I seems that you retrieve access token using refresh token. 2. For example, how about the method without ``google-api-python-client``? So it exports files using only ``request`` and refresh token. – Tanaike Sep 04 '17 at 02:17
  • @Tanaike Actually I am not sure if it is access token or refresh token. According to doc it should return me an access token. No I haven't tried just http. Would you like to continue the discussion as comment to my questions? It might help other SOers to understand the issue. – Anthony Kong Sep 04 '17 at 02:20
  • @Anthony Kong I will move to [your question](https://stackoverflow.com/questions/45429618/unable-to-export-a-google-slide-as-pdf-format-on-server-side-likely-google-auth) from now. – Tanaike Sep 04 '17 at 02:21