小编Gev*_*ezo的帖子

如何使用 python 请求通过 Google Drive API 上传和删除文件

我正在尝试使用请求将图像上传到谷歌驱动器,但它不起作用,因为请求不断给我状态 401(凭据错误)。我正在使用给我的访问令牌,所以我不知道发生了什么。

这是我的代码:

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)
Run Code Online (Sandbox Code Playgroud)

python python-requests google-drive-api

5
推荐指数
1
解决办法
1345
查看次数

标签 统计

google-drive-api ×1

python ×1

python-requests ×1