gpt4 book ai didi

python - 为什么此 mime 类型被视为对创建 Google 文档无效?

转载 作者:行者123 更新时间:2023-11-28 18:33:48 24 4
gpt4 key购买 nike

我为 python-quickstart 创建了一个“包装器”类代码。它在我的类里面运行良好,除非我更改 mime 类型。在他们的代码中,他们创建了一个纯文本文档,但我正在尝试从我的代码创建一个 Google Docs 文件。当我尝试运行此代码时,我收到一个 HttpError 400 消息,指出我的 MIME 类型无效。我究竟做错了什么?

这是我的代码:

import pprint
import httplib2
import googleapiclient.discovery
import googleapiclient.http
import googleapiclient.errors
import oauth2client.client


class DriveClient():
def __init__(self):
self.oauth2_scope = 'https://www.googleapis.com/auth/drive'
self.client_secrets = 'client_secrets.json'
self.mimetype = 'application/vnd.google-apps.document'
self.flow = self.set_flow()
self.drive_service = self.authorize_url()

def set_flow(self):
flow = oauth2client.client.flow_from_clientsecrets(self.client_secrets,
self.oauth2_scope)
flow.redirect_uri = oauth2client.client.OOB_CALLBACK_URN
return flow

def authorize_url(self):
authorize_url = self.flow.step1_get_authorize_url()
print('Go to the following link in your browser: ' + authorize_url)
code = input('Enter verification code: ').strip()
credentials = self.flow.step2_exchange(code)

http = httplib2.Http()
credentials.authorize(http)
drive_service = googleapiclient.discovery.build('drive', 'v2',
http=http)
return drive_service

def push_file(self, file_src, title, description=''):
media_body = googleapiclient.http.MediaFileUpload(
file_src, mimetype=self.mimetype, resumable=True)
body = {
'title': title,
'description': description
}
try:
new_file = self.drive_service.files().insert(body=body,
media_body=media_body
).execute()
pprint.pprint(new_file)
except googleapiclient.errors.HttpError as error:
print('An error occured: %s' % error)


if __name__ == '__main__':
d = DriveClient()
d.push_file('document.txt', 'mTitle', 'mDescription')

最佳答案

尝试将 mime 类型设置为源文档的类型,例如 application/mswordapplication/vnd.oasis.opendocument.text 等。Google 需要知道传入文档的格式是什么,然后它将选择要创建哪种谷歌文档。

关于python - 为什么此 mime 类型被视为对创建 Google 文档无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34301944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com