【问题标题】:GmailUserSocialAuth email message returns empty message but has snippetGmailUserSocialAuth 电子邮件返回空消息,但有片段
【发布时间】:2016-11-22 17:56:06
【问题描述】:

我在 python 中使用 Gmail API,收到一个请求:

gmail_auth = GmailUserSocialAuth.objects.filter(uid='...')[0]
response = gmail_auth.request('get', '...')
data = response.json()

response - gmail_auth.request('get', '/%s' % data['messages'][0]['id']
message = response.json()

当我打印出消息时,我会得到包含所有字段等的大型对象。通过其中一条消息,我得到以下回复:

{
    ... # a lot of fields
    u'sizeEstimate': 10100,
    'html_body': '',
    'decoded_body': '',
    u'snippet': u'Hi —, <content of email>. On Jun 30, 2016..., Ofek Gila <...> wrote: <content of previous email in thread>.',
}

无论如何,问题是我知道这封电子邮件是写的,因为它出现在 sn-p 中,但它没有出现在消息对象的其他任何地方。

知道会发生什么吗?

提前致谢!

【问题讨论】:

    标签: python email gmail gmail-api


    【解决方案1】:

    尝试使用Python sample code 中所述的get 方法。 这是一个sn-p:

    def GetMimeMessage(service, user_id, msg_id):
      """Get a Message and use it to create a MIME Message.
    
      Args:
        service: Authorized Gmail API service instance.
        user_id: User's email address. The special value "me"
        can be used to indicate the authenticated user.
        msg_id: The ID of the Message required.
    
      Returns:
        A MIME Message, consisting of data from Message.
      """
      try:
        message = service.users().messages().get(userId=user_id, id=msg_id,
                                                 format='raw').execute()
        print 'Message snippet: %s' % message['snippet']
        msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))
        mime_msg = email.message_from_string(msg_str)
        return mime_msg
      except errors.HttpError, error:
        print 'An error occurred: %s' % error
    

    您也可以查看SO thread 和this one 以获得更多信息。

    【讨论】:

    • 感谢您的反馈,我一定会查看的!
    • 这似乎没有帮助,它给了我同样的问题。我知道对于大多数消息(除了一个),它都可以正常工作。只有一个示例消息不存在,但 sn-p 显示消息。任何线索可能导致这种情况?感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    相关资源
    最近更新 更多