我一直在嘗試使用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())
謝謝你讓我朝着正確的方向發展,我花了很長時間才弄清楚發生了什麼。 –