2014-04-07 137 views
1

我一直在尝试使用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()) 

回答

0

大量实验后,我终于想通了,它怎么做的。所以在这里它是在同一位置的人,因为我。

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 = '[email protected]' 
     m.to = '[email protected]' 
     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()) 
1

那么在m.attachments属性,你应该像你一样对每个附件一元组,第一个元素是文档的标题,而第二个文档数据本身。

就你而言,你的文档数据只是一个字符串,它不是一个文件,所以这是你的问题。 您应该首先从驱动器检索pdf文件,然后将其作为附件。

您可以检查here如何下载使用将downloadURL属性文件内容(或者你的情况exportLinks。

另外,请确保您设置适当的扩展您的文件。文件不带扩展名或某些具体的扩充功能,而不能作为附件发送。Check this

希望这有助于。

+0

谢谢你让我朝着正确的方向发展,我花了很长时间才弄清楚发生了什么。 –

相关问题