5

I'm trying to upload a image to google drive using requests, but it's not working because the requests keep giving me a status 401 (wrong credentials). I am using the access token that was given to me, so I don't know what is going on.

There's my code:

tokendrive = TOKEN_DRIVE
url = "site_url"
myid = MY_ID
subid = MY_SUB_ID
r = requests.get(url)
headers = {'Authorization': 'Bearer ' + tokendrive} 
para = {"name": submission.id + ".png",
        "parents": [myid]}
files = {"data": ("metadata", json.dumps(para), "application/json; charset=UTF-8"),
         "file": io.BytesIO(requests.get(url).content)}
response = requests.post("https://www.googleapis.com/upload/drive/v3/files",headers=headers,files=files)
print(response.text)
Gevezo
  • 364
  • 3
  • 17

1 Answers1

8

I believe your goal and your current situation as follows.

  • You want to upload a file to Google Drive.
  • You want to delete a file on Google Drive.
  • You want to achieve this using request module of python.
  • From your endpoint, you want to use Drive API v3.
  • Your access token can be used for uploading and deleting the file using Drive API.

Modification points:

  • If your access token can be used for uploading a file to Google Drive, it is required to modify the script for using the access token. In this case, please modify Token to Bearer. I thought that the reason of your error might be due to this.
  • When Drive API v3 is used, the property of parents is "parents": [myid]. And in the current stage, please use one folder ID here.
  • In the case of Drive API v3, the filename can be given with name instead of title. title is used for Drive API v2.

When above points are reflected to your script, it becomes as follows.

Modified script:

This modified script uploads a file of to Google Drive. Before you use this script, please confirm the variables you want to use, again.

headers = {'Authorization': f'Bearer {tokendrive}'} # or 'Bearer ' + tokendrive
para = {
    "name": "image_url.jpg",
    "parents": [myid]
}
files = {
    "data": ("metadata", json.dumps(para), "application/json; charset=UTF-8"),
    "file": io.BytesIO(requests.get(submission.url).content)
}
response = requests.post(
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
    headers=headers,
    files=files
)
print(response.text)
  • In this script, the following value is returned.

      {
       "kind": "drive#file",
       "id": "###",
       "name": "image_url.jpg",
       "mimeType": "image/jpeg"
      }
    
  • In this modified script, the maximum file size for uploading is 5 MB. When you want to upload a file over 5 MB, please use resumable upload. In that case, I think that this thread might be useful. Ref

Sample script:

This sample script deletes a file on Google Drive.

fileId = '###' # Please set the file ID you want to delete.
headers = {'Authorization': f'Bearer {tokendrive}'} # or 'Bearer ' + tokendrive
response = requests.delete(
    "https://www.googleapis.com/drive/v3/files/" + fileId,
    headers=headers,
)
print(response.text)
  • In this case, no value is returned. This is the current specification.

  • IMPORTANT: This script completely deletes the file of fileId. So please be careful this. I would like to recommend to test using a sample file.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • @Gevezo Thank you for replying. I apologize for the inconvenience. From `I still have the same problem with authentication`, in that case, your access token might not be used for uploading a file. So can you confirm it again? And also, can you add your current script for replicating your issue? If you can do it, please add it to your question. By this, I would like to confirm it. – Tanaike Feb 17 '21 at 00:31
  • @Gevezo And also, unfortunately, I cannot understand about `Maybe I need more information about the specific folder ?`. If you tested my 1st sample script and you cannot understand about `myid`, how about using `root` as `myid` or removing `"parents": [myid]`? By this, the uploaded file is put to the root folder. By this, when your access token can be used for uploading a file, the script works. – Tanaike Feb 17 '21 at 00:31
  • @Gevezo Thank you for replying and adding your current script. When I saw your script, I noticed an important modification point. In your script, `headers = {'Authorization': 'Bearer' + tokendrive}` is used as the header. If `tokendrive` is the access token, please add a space like 'Bearer '. Your script is `'Bearer'`. I thought that this is the reason of your issue. So your modified script becomes `headers = {'Authorization': 'Bearer ' + tokendrive}`. By this modification, please test it again. – Tanaike Feb 17 '21 at 12:28
  • @Gevezo Thank you for replying. I'm glad your issue was resolved. About your additional new 2 questions, I have to apologize for my poor English skill. About your 2nd question, unfortunately, I cannot understand about `that means the file is too large ?`. Can I ask you about the detail of your current situation? About your 3rd question, it's yes. – Tanaike Feb 17 '21 at 13:10
  • @Gevezo Thank you for replying. I'm glad your 2nd and 3rd questions were resolved. And also, I apologize for the inconvenience. About your 4th question, I would like to support you. But I cannot replicate your situation. This is due to my poor skill. I deeply apologize for this. In order to correctly understand about your current issue, can you provide your current script and the sample PNG file for replicating your issue? By this, I would like to confirm it. If you can cooperate to resolve your issue, I'm glad. Can you cooperate to resolve it? – Tanaike Feb 18 '21 at 00:42
  • @Gevezo Thank you for replying. I have to apologize for my poor English skill. Unfortunately, I cannot understand about your current situation. Can I ask you about the detail of your current issue and your current goal? By the way, your current goal was changed from your 1st question? – Tanaike Mar 04 '21 at 00:23
  • @Gevezo Thank you for replying. I apologize for the inconvenience again. Unfortunately, when I tested my sample script for uploading a PNG file, no error occurs. I deeply apologize for this situation. So in order to correctly replicate your situation, can you provide the sample PNG file for replicating your issue? By this, I would like to confirm it. By the way, about `the .png file is not valid (it does not open as an image)`, can you also provide the file? – Tanaike Mar 04 '21 at 13:27
  • @Gevezo Thank you for replying and adding the sample PNG files. When I saw your files, the file size of all files is `0`. I think that this is the reason of your issue. I apologize for this situation. In my environment. when the PNG file which has the correct PNG data is uploaded using my sample script, I can confirm that the PNG file can be seen. Can you confirm this again? – Tanaike Mar 04 '21 at 23:23
  • @Gevezo Thank you for replying. I have to apologize for my poor English skill again. I thought that your sample PNG files are files you want to upload. From your replying, I understood those are the uploaded files.When you had uploaded those files from several URLs, I think that the reason of your issue is due to the URLs. In order to check whether the script correctly works, can you test the following situation? – Tanaike Mar 06 '21 at 01:40
  • @Gevezo For example, using my sample script, when the URL of `io.BytesIO(requests.get(submission.url).content)` is `https://stackoverflow.design/assets/img/logos/so/logo-stackoverflow.png` which is the logo of Stackoverflow, I can confirm that the correct image can be uploaded. When you test this and the same result that the file has no data size occurs, I think that your script has an issue. At that time, can you provide your script for replicating the issue? By this, I would like to confirm it. – Tanaike Mar 06 '21 at 01:40
  • 1
    http://collabedit.com/u98t3, I'm using this exact script and having the same problem, maybe I'm writing something wrong ? – Gevezo Mar 06 '21 at 17:42
  • @Gevezo Thank you for replying and adding more information. When I saw your current script, I confirmed that you don't correctly use my proposed script. In your situation, the file is uploaded with `multipart/form-data`. In this case, the endpoint is `https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart`. But you are using `https://www.googleapis.com/upload/drive/v3/files` as the endpoint. This is the reason of your issue. I think that your issue is due to my poor English skill. I deeply apologize for this. Please modify the endpoint and test it again. – Tanaike Mar 07 '21 at 00:41
  • 1
    Thank you very much ! This solved the problem, and again, thanks for the patience. – Gevezo Mar 08 '21 at 13:05
  • @Gevezo Thank you for replying. I'm glad your issue was resolved. – Tanaike Mar 09 '21 at 00:09