How do I upload file to google drive using drive API using python?(如何使用 python 的驱动器 API 将文件上传到谷歌驱动器?)
问题描述
我想使用它的 API 将文件上传到谷歌驱动器,我正在使用代码
I want to upload a file to google drive using its API, I am using the code
def newer():
url= 'https://USERNAME:PASSWORD@www.googleapis.com/upload/drive/v3/files?uploadType=media'
data='''{{
"name":"testing.txt",
}}'''
response = requests.post(url, data=data)
print response.text
但是,我收到如下响应错误消息.
However, I am getting response error message as below.
{错误":{错误":[{域":全球",原因":authError","message": "此 API 不支持 HTTP 基本身份验证",位置类型":标题","location": "Authorization" } ], "code": 401, "message": "HTTP Basic Authentication is not supported for this API" } }
{ "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "HTTP Basic Authentication is not supported for this API", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "HTTP Basic Authentication is not supported for this API" } }
还有其他方法可以使用 python 完成我的工作吗?
Is there some other way to do my job using python.
我是否需要登录谷歌云才能访问 API 以获取身份验证令牌或凭据
Should I need to sign in to google cloud to access API for authentication token or credentials
推荐答案
终于明白如何使用api上传文件到google drive了.
Finally I Understood how do I upload file to google drive using api.
首先你需要安装 python 库,它提供了使用驱动 api 的方法.安装库: pip install google-api-python-client然后代码如下.
first you need to install python library which gives the methods to use drive api. installing the library: pip install google-api-python-client then code as below.
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from apiclient.http import MediaFileUpload,MediaIoBaseDownload
import io
# Setup the Drive v3 API
SCOPES = 'https://www.googleapis.com/auth/drive.file'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
drive_service = build('drive', 'v3', http=creds.authorize(Http()))
上面的代码片段是创建对象/变量,允许您使用正确的凭据进入驱动器.这里 drive_service
可以做到这一点.
above code snippet is to create object/variable which allow you to get inside the drive with a right credential. here drive_service
does that work.
文件上传代码如下.
def uploadFile():
file_metadata = {
'name': 'fileName_to_be_in_drive.txt',
'mimeType': '*/*'
}
media = MediaFileUpload('Filename_of_your_local_file.txt',
mimetype='*/*',
resumable=True)
file = drive_service.files().create(body=file_metadata, media_body=media, fields='id').execute()
print ('File ID: ' + file.get('id'))
文件 ID 很重要,因为如果要从驱动器下载文件,则需要文件 ID.
The file ID is important because if you want to download the file from the drive you need the file ID.
这篇关于如何使用 python 的驱动器 API 将文件上传到谷歌驱动器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 python 的驱动器 API 将文件上传到谷歌驱


- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 沿轴计算直方图 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01