我一直在尝试使用Jinja2创建一个模板化的Google Drive文档,并最终将该文档作为附加的PDF文档通过电子邮件发送。从Google Drive附加文件
到目前为止,我已经设法做到了最多,但现在我坚持在附件部分。我收到错误“InvalidAttachmentTypeError:Invalid attachment type”。
有没有一种方法来改善这一点,使其效率更高一点。
class Upload(webapp2.RequestHandler):
@decorator.oauth_required
def get(self):
if decorator.has_credentials():
try:
body = {'title': 'My New Text Document',
'description': 'Hello World'}
template = JINJA_ENVIRONMENT.get_template('document.html')
template_values = {'name': 'Simon'}
fh = StringIO.StringIO(template.render(template_values))
media_body = MediaIoBaseUpload(fh,
mimetype='text/html',
resumable=False)
http = httplib2.Http(memcache)
http = decorator.http()
service = discovery.build('drive', 'v2', http=http)
file = service.files().insert(body=body,
media_body=media_body,
convert=True).execute(http=http)
m = mail.EmailMessage()
m.sender = '[email protected]'
m.to = '[email protected]'
m.subject = 'My Subject'
m.html = '<p>My body.</p>'
m.attachments = [(file['title'],
file['exportLinks']['application/pdf'])]
m.send()
self.redirect('/')
except client.AccessTokenRefreshError:
self.redirect('/')
else:
self.redirect(decorator.authorize_url())
谢谢你让我朝着正确的方向发展,我花了很长时间才弄清楚发生了什么。 –