我已经为python-quickstart代码创建了一个“包装器”类。它在我的课堂上运行良好,除非我改变mime类型。在他们的代码中,他们创建了一个纯文本文档,但我正试图从我的代码中创建一个googledocs文件。当我尝试运行这段代码时,我收到一个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文档的类型。你知道吗相关问题 更多 >
编程相关推荐