How to get gmail messages with the Flask OAuthlib?

449 views Asked by At

I'm building a website using flask, in which people can authorize my website for their gmail account using the Flask-OAuthlib. The initial authorization works fine and after that I can get the userinfo like this:

userinfo = google.get('userinfo')
print userinfo.raw_data  # prints out a raw json object with the userinfo

I then tried getting a list of messages. I can do it using a raw url like this:

import requests
emailList = requests.get('https://www.googleapis.com/gmail/v1/users/' + userId + '/messages?access_token=' + accessToken)
print emailList.json()  # prints out a list of message `id`s and `threadId`s

But I don't understand how I can do this using the Flask OAuthlib. I tried some of the following things, but none of them works:

messages = google.get('messages')
messages = google.get('/users/' + userId + '/messages')
messages = google.get('users')
messages = google.get('usermessages')  # I know this sucks, but I'm pretty desperate..

Does anybody know how I can get the list of messages (and after that the individual messages) with this Flask OAuthlib? All tips are welcome!

bonus question: Is there a way to search messages by string and by whether they have an attachment or not?

1

There are 1 answers

0
arshpreet On

You can use Imap to login If you are able to get access_token and user Email.

After getting the Access_token using Flask OAuthlib you can go for following discussion:

stackoverflow.com/questions/9134491/access-gmail-imap-with-oauth-2-0-access-token

On the other hand this tutorial also describes your exact query:

https://medium.com/@etothemanders/using-flask-oathlib-with-the-gmail-api-47079e85202d

I have not tried it but Going to and confirm here when done.