简体   繁体   English

如何使用 python 请求使用谷歌驱动器 API 上传和删除文件

[英]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.我相信你的目标和你目前的情况如下。

  • You want to upload a file to Google Drive.您想将文件上传到 Google Drive。
  • You want to delete a file on Google Drive.您想删除 Google Drive 上的文件。
  • You want to achieve this using request module of python.您想使用 python 的request模块来实现此目的。
  • From your endpoint, you want to use Drive API v3.从您的端点,您想使用 Drive API v3。
  • Your access token can be used for uploading and deleting the file using Drive API.您的访问令牌可用于使用 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.如果您的访问令牌可用于将文件上传到 Google Drive,则需要修改脚本以使用访问令牌。 In this case, please modify Token to Bearer .在这种情况下,请将Token修改为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] .使用 Drive API v3 时, parents的属性是"parents": [myid] And in the current stage, please use one folder ID here.在当前阶段,请在此处使用一个文件夹 ID。
  • In the case of Drive API v3, the filename can be given with name instead of title .在 Drive API v3 的情况下,文件名可以用name代替title title is used for Drive API v2. title用于驱动器 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.此修改后的脚本将文件上传到 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参考

Sample script:示例脚本:

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.我想建议使用示例文件进行测试。

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 驱动器 api 将 csv 文件上传到谷歌驱动器 - How to upload csv files to google drive using python drive api 如何使用 Python 通过 api 将文件上传到谷歌驱动器? - How to upload files into google drive via api using Python? python中无法使用Google Drive上传文件 API - Unable to upload files using Google Drive API in python 使用 API for Python 从谷歌驱动器中删除文件/文件夹 - Delete files/folders from google drive using API for Python 如何使用 Python 将文件插入/上传到 Google Drive API v3 中的特定文件夹 - How can I insert/upload files to a specific folder in Google Drive API v3 using Python Python-如何使用 Google Drive API 将文件上传到一个文件夹? - Python- How to upload files to one folder using Google Drive API? 如何使用 Google Drive API 一次删除多个文件 - How to delete multiple files at once using Google Drive API 如何使用python和Google云端硬盘上传到文件夹并替换文件? - how to upload to folder and replace files using python and Google Drive? Python:如何使用 folderId 将文件上传到 Google Drive 中的特定文件夹 - Python: How to upload files to a specific folder in Google Drive using folderId 如何使用python Telegram Bot API将文件上传到Google驱动器 - How to upload a file to google drive using python Telegram Bot API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM