Google Drive API v3文件通过python上传错误 [英] Google Drive api v3 file upload errors via python

查看:149
本文介绍了Google Drive API v3文件通过python上传错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试根据 https://developers.google.com/drive/api/v3/manage-uploads#python_1 .下面给出了一个示例代码,我被当前情况所困扰.

I was trying to upload a resumable file upload on google api drive as per the documentation from https://developers.google.com/drive/api/v3/manage-uploads#python_1 . A sample code is given below, I am stuck with the current situation.

SCOPES = ['https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive.appdata']
credentials = ServiceAccountCredentials.from_json_keyfile_name('json-file', SCOPES)
http=Http()
http.redirect_codes = http.redirect_codes - {308}
http_auth = credentials.authorize(http)
drive_service = build('drive', 'v3', http=http_auth,cache_discovery=False)

parent_id="folder-id"
source='/root/test.tar'
target='test.tar'
file_metadata = {'name': target, 'parents': [parent_id], 'mimeType': 'application/vnd.google-apps.file'}
media = MediaFileUpload(source,mimetype='application/vnd.google-apps.file',chunksize=1024*1024,resumable=True)
putfile=drive_service.files().create(body=file_metadata,media_body=media,fields='id').execute()
dest_id=putfile.get('id')

这些行正在处理以下错误.我对此一无所知.

These lines are trowing the following errors. I have no idea about it.

提高HttpError(resp,content,uri = self.uri) googleapiclient.errors.HttpError:< HttpError 400,当请求

raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/upload/drive/v3/files?fields=id&alt=json&uploadType=resumable returned "Bad Request">

我想知道是什么导致了这个问题?

I wonder what is making this issue ?

推荐答案

您缺少块大小.

parent_id = "folder-id"
source = '/root/test.tar'
target = 'test.tar'
file_metadata = {'name': target, 'parents': [parent_id], 
                 'mimeType': 'application/vnd.google-apps.file'}
media = MediaFileUpload(source, mimetype='application/vnd.google-apps.file', 
             chunksize=1024*1024, resumable=True)
putfile = drive_service.files().create(body=file_metadata,
                         media_body=media,fields='id').execute()
dest_id = putfile.get('id')

这篇关于Google Drive API v3文件通过python上传错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆