After a lot of experimenting I have finally figured out how its done. So here it is for anyone in the same position as I.
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)
download_url = file['exportLinks']['application/pdf']
resp, content = service._http.request(download_url)
m = mail.EmailMessage()
m.sender = 'myfromemailaaddress@gmail.com'
m.to = 'mytoemailaddress@gmail.com'
m.subject = 'My Subject'
m.html = '<p>My body.</p>'
m.attachments = [('myfile.pdf', str(content))]
m.send()
self.redirect('/')
except client.AccessTokenRefreshError:
self.redirect('/')
else:
self.redirect(decorator.authorize_url())