Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 74961bf

Browse files
committed
Added documentation for the IMAP and SMTP XOAUTH clients.
1 parent aa5a408 commit 74961bf

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,63 @@ and code here might need to be updated if you are using Python 2.6+.
295295

296296
* You'll likely want to set `LOGIN_URL` to `/login/` so that users are properly redirected to your Twitter login handler when you use `@login_required` in other parts of your Django app.
297297
* You can also set `AUTH_PROFILE_MODULE = 'mytwitterapp.Profile'` so that you can easily access the Twitter OAuth token/secret for that user using the `User.get_profile()` method in Django.
298+
299+
# XOAUTH for IMAP and SMTP
300+
301+
Gmail supports OAuth over IMAP and SMTP via a standard they call XOAUTH. This allows you to authenticate against Gmail's IMAP and SMTP servers using an OAuth token and secret. It also has the added benefit of allowing you to use vanilla SMTP and IMAP libraries. The `python-oauth2` package provides both IMAP and SMTP libraries that implement XOAUTH and wrap `imaplib.IMAP4_SSL` and `smtplib.SMTP`. This allows you to connect to Gmail with OAuth credentials using standard Python libraries.
302+
303+
## IMAP
304+
305+
import oauth2 as oauth
306+
import oauth2.clients.imap as imaplib
307+
308+
# Set up your Consumer and Token as per usual. Just like any other
309+
# three-legged OAuth request.
310+
consumer = oauth.Consumer('your_consumer_key', 'your_consumer_secret')
311+
token = oauth.Token('your_users_3_legged_token',
312+
'your_users_3_legged_token_secret')
313+
314+
# Setup the URL according to Google's XOAUTH implementation. Be sure
315+
# to replace the email here with the appropriate email address that
316+
# you wish to access.
317+
url = "https://mail.google.com/mail/b/[email protected]/imap/"
318+
319+
conn = imaplib.IMAP4_SSL('imap.googlemail.com')
320+
conn.debug = 4
321+
322+
# This is the only thing in the API for impaplib.IMAP4_SSL that has
323+
# changed. You now authenticate with the URL, consumer, and token.
324+
conn.authenticate(url, consumer, token)
325+
326+
# Once authenticated everything from the impalib.IMAP4_SSL class will
327+
# work as per usual without any modification to your code.
328+
conn.select('INBOX')
329+
print conn.list()
330+
331+
332+
## SMTP
333+
334+
import oauth2 as oauth
335+
import oauth2.clients.smtp as smtplib
336+
337+
# Set up your Consumer and Token as per usual. Just like any other
338+
# three-legged OAuth request.
339+
consumer = oauth.Consumer('your_consumer_key', 'your_consumer_secret')
340+
token = oauth.Token('your_users_3_legged_token',
341+
'your_users_3_legged_token_secret')
342+
343+
# Setup the URL according to Google's XOAUTH implementation. Be sure
344+
# to replace the email here with the appropriate email address that
345+
# you wish to access.
346+
url = "https://mail.google.com/mail/b/[email protected]/smtp/"
347+
348+
conn = smtplib.SMTP('smtp.googlemail.com', 587)
349+
conn.set_debuglevel(True)
350+
conn.ehlo('test')
351+
conn.starttls()
352+
353+
# Again the only thing modified from smtplib.SMTP is the authenticate
354+
# method, which works identically to the imaplib.IMAP4_SSL method.
355+
conn.authenticate(url, consumer, token)
356+
357+

0 commit comments

Comments
 (0)