[英]How to upload and delete files with google drive API using python requests
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).我正在尝试使用请求将图像上传到谷歌驱动器,但它不起作用,因为请求不断给我一个状态 401(错误的凭据)。 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 = "ya29.A0AfH6SMDn1ti-5u8gjLQhHf3ffILkR2_8mSRBoap7hY8ZuIGoFomPhZJAi_hVgORZkBJtBeTDhlPnOeksNZvwrUPgBcuHle5BMqULMBY7Y97fIaMGuOqSAQIrSyXnqLbtDLEwrnCAXfHHSVxtWAbX80pHkarG"
url = "https://www.reddit.com/r/LiminalSpace/comments/lwoipa/i_guess_nobody_woke_me_up/"
myid = "1QoepljKHkjSarx0kCcwEnHjRA5fBP4sU"
subid = lwoipa
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)
I believe your goal and your current situation as follows.我相信你的目标和你目前的情况如下。
request
module of python.request
模块来实现此目的。Token
to Bearer
.Token
修改为Bearer
。 I thought that the reason of your error might be due to this.parents
is "parents": [myid]
.parents
的属性是"parents": [myid]
。 And in the current stage, please use one folder ID here.name
instead of title
.name
代替title
。 title
is used for Drive API v2. title
用于驱动器 API v2。 When above points are reflected to your script, it becomes as follows.当以上几点反映到您的脚本时,它变成如下。
This modified script uploads a file of to Google Drive.此修改后的脚本将文件上传到 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.在这个修改后的脚本中,上传的最大文件大小为 5 MB。 When you want to upload a file over 5 MB, please use resumable upload.
当您要上传超过 5 MB 的文件时,请使用可恢复上传。 In that case, I think that this thread might be useful.
在那种情况下,我认为这个线程可能有用。 Ref
参考
This sample script deletes a file on Google Drive.此示例脚本删除 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
.重要提示:此脚本完全删除
fileId
的文件。 So please be careful this.所以请注意这一点。 I would like to recommend to test using a sample file.
我想建议使用示例文件进行测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.