为什么此 mime 类型被视为对创建 Google 文档无效?
Why is this mime type deemed invalid for creating a Google Doc?
我为 python-quickstart 代码创建了 "wrapper" class。它在我的 class 中工作正常,除非我更改 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/msword
、application/vnd.oasis.opendocument.text
等。Google 需要知道传入文档的格式是什么,然后它将选择要创建哪种 google 文档。
我为 python-quickstart 代码创建了 "wrapper" class。它在我的 class 中工作正常,除非我更改 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/msword
、application/vnd.oasis.opendocument.text
等。Google 需要知道传入文档的格式是什么,然后它将选择要创建哪种 google 文档。