2

I am stuck at this point. Can somebody help me with how to upload files to a folder? using google drive API I am new using API and sending HTTPS requests. I added the parents section but it still upload the file to root.

import sys
import json
import requests
import sys

Refreshtoken = ""
params = {
       "grant_type": "refresh_token",
       "client_id": "xxxxx",
       "client_secret": "xxxxxxxxxxx",
       "refresh_token": "xxxxxxxxxxx"
       }

authorization_url = "https://www.googleapis.com/oauth2/v4/token"

r = requests.post(authorization_url, data=params)

if r.ok:
   token = str((r.json()['access_token']))
   Refreshtoken = Refreshtoken + token
else:
  print("Failed")
  sys.exit()


headers = {
       "Authorization": "Bearer " + str(Refreshtoken),
   }


metadata= {
   'name': "Test",
   'parents':"1vXn346cBsarvkPthqtI1OLzsMzeQQlBX"
}

files = {
   'data': ('metadata', json.dumps(metadata), 'application/json; charset=UTF-8'),
   'file': open("./test.txt", "rb"),
   }

r2= requests.post(
   "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
   headers= headers,
   files= files
)


print(r2.text)```

1 Answer 1

3

Parents is an array files.create

metadata= {
   'name': "Test",
   'parents': ["1vXn346cBsarvkPthqtI1OLzsMzeQQlBX"]
}

Have you considered using the Google Apis python client library?

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, but I would rather stick with the HTTP request method. It's so straightforward. I've been working on it for hours now. And thanks again
"Authorization": "Bearer " + str(Refreshtoken), <-- bearer token is the access token not the refresh token.
Oh yha Sorry. Can you please tell is there is a way to download files also only use HTTP requests without google python lib?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.