如何使用 google api 将文件上传到文件夹

How to upload file to folder using google api

我卡在这一点上了。有人可以帮助我如何将文件上传到文件夹吗?使用 google 驱动器 API 我是新手,使用 API 并发送 HTTPS 请求。我添加了 parents 部分,但它仍然将文件上传到根目录。

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)```

Parents 是一个数组 files.create

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

您是否考虑过使用 Google Apis python client library