12

I am using Google's Oauth 2.0 to get the user's access_token, but I dont know how to use it with imaplib to access inbox.

Kuldeep Kapade
  • 1,095
  • 3
  • 12
  • 17
  • Did you find a solution? I am looking at re-writing some of the current libraries to just use the access_token. – Wasauce Jul 09 '12 at 00:35

4 Answers4

11

Below is the code for IMAP with oauth 2.0

email = 'k@example.com'
access_token = 'vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEuY29tCg'
auth_string = 'user=%s\1auth=Bearer %s\1\1' % (email, access_token)

imap_conn = imaplib.IMAP4_SSL('imap.gmail.com')
imap_conn.debug = 4
imap_conn.authenticate('XOAUTH2', lambda x: auth_string)
imap_conn.select('INBOX')

for more details see the library code.

Erik Cederstrand
  • 9,643
  • 8
  • 39
  • 63
user303110
  • 5,976
  • 1
  • 16
  • 4
1

Currently you can use OAuth 1.0 to access Gmail over IMAP and SMTP, but OAuth 2.0 is not yet supported. Here is a link to more information: https://developers.google.com/google-apps/gmail/oauth_overview

Cain Wong
  • 11
  • 1
1

This is something I've been kicking around. I didn't want to juggle refreshing access tokens and what not myself -- I also found there was too much boilerplate code in the Google example. I decided just to write very simple wrappers that allow for OAuth2 IMAP and SMTP that utilize Credentials and Flow objects from google-api-python-client.

Hopefully this helps somebody.

https://github.com/richieforeman/oauth2gmail

Richie Foreman
  • 367
  • 1
  • 6
-1

IMAP does not support accessing inbox without password -> so imaplib doesnt

Ramchandra Apte
  • 4,033
  • 2
  • 24
  • 44