0

I'm working with the Gmail API in python, getting a request with:

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()

When I print out the message, I get large large objects with all the fields and such. With one of the messages, I get this response:

{
    ... # 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>.',
}

Anyway, the issue is that I know the email was written because it appears in the snippet, but it doesn't show up anywhere else in the message object.

Any idea what could be happening?

Thanks in advance!

Ofek Gila
  • 693
  • 1
  • 10
  • 21

1 Answers1

1

Try to use the get method as stated in the Python sample code. Here's a snippet:

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

You may also check this SO thread and this one for additional insight.

Community
  • 1
  • 1
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • thanks for your feedback, I'll be sure to check it out! – Ofek Gila Jul 21 '16 at 00:38
  • this doesn't seem to help, it's giving me the same issue. I know that with most messages (all but one), it works fine. Just one example has the message non-existent, but a snippet showing the message. Any clue what could be causing that? Thanks for your help! – Ofek Gila Jul 21 '16 at 18:39