2014-04-07 134 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

謝謝你讓我朝着正確的方向發展,我花了很長時間才弄清楚發生了什麼。 –

相關問題