From 7a19c5db2515591233c5c28e7d11e55ddf13431b Mon Sep 17 00:00:00 2001 From: Adam Coddington Date: Sat, 31 May 2014 19:04:24 -0700 Subject: [PATCH 1/4] Minor documentation tweaks. --- docs/topics/mailbox_types.rst | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/topics/mailbox_types.rst b/docs/topics/mailbox_types.rst index bb8122c..e3c9244 100644 --- a/docs/topics/mailbox_types.rst +++ b/docs/topics/mailbox_types.rst @@ -12,7 +12,7 @@ POP3 and IMAP as well as local file-based mailboxes. ============ ================ ================================================================================================================================================================= POP3 ``pop3://`` Can also specify SSL with ``pop3+ssl://`` IMAP ``imap://`` Can also specify SSL with ``imap+ssl://``, or specify a folder to save processed messages into by appending ``?archive=my_archive_folder`` to the end of the URI. - Gmail IMAP ``gmail+ssl://`` Password is only used as a fallback if oauth2 fails + Gmail IMAP ``gmail+ssl://`` Uses OAuth authentication for Gmail's IMAP transport. See :ref:`gmail-oauth` for details. Maildir ``maildir://`` Mbox ``mbox://`` Babyl ``babyl://`` @@ -62,16 +62,21 @@ would enter the following as your URI:: imap+ssl://youremailaddress%40gmail.com:1234@imap.gmail.com?archive=Archived +.. _gmail-oauth: + Gmail IMAP with Oauth2 authentication ------------------------------------- -Gmail supports using oauth2 for authentication_ which is more secure. +For added security, Gmail supports using OAuth2 for authentication_. To handle the handshake and storing the credentials, use python-social-auth_. .. _authentication: https://developers.google.com/gmail/xoauth2_protocol .. _python-social-auth: http://psa.matiasaguirre.net/ -The Gmail Mailbox is also a regular IMAP mailbox, but the password will be ignored if oauth2 succeeds. It can fall back to password as needed. +The Gmail Mailbox is also a regular IMAP mailbox, +but the password you specify will be ignored if OAuth2 authentication succeeds. +It will fall back to use your specified password as needed. + Build your URI accordingly:: gmail+ssl://youremailaddress%40gmail.com:oauth2@imap.gmail.com?archive=Archived From d1485d9cd5bd55a0832536c067c298c5ebbb3be7 Mon Sep 17 00:00:00 2001 From: Adam Coddington Date: Sun, 1 Jun 2014 14:24:34 -0700 Subject: [PATCH 2/4] Replacing print statements with logger.info --- django_mailbox/google_utils.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/django_mailbox/google_utils.py b/django_mailbox/google_utils.py index a668ff6..4d6b773 100644 --- a/django_mailbox/google_utils.py +++ b/django_mailbox/google_utils.py @@ -1,6 +1,11 @@ -from social.apps.django_app.default.models import UserSocialAuth -import requests +import logging + from django.conf import settings +import requests +from social.apps.django_app.default.models import UserSocialAuth + + +logger = logging.getLogger(__name__) class AccessTokenNotFound(Exception): @@ -50,17 +55,16 @@ def google_api_get(email, url): Authorization="Bearer %s" % get_google_access_token(email), ) r = requests.get(url, headers=headers) - print "I got a %s" % r.status_code + logger.info("I got a %s", r.status_code) if r.status_code == 401: # Go use the refresh token refresh_authorization(email) r = requests.get(url, headers=headers) - print "I got a %s" % r.status_code + logger.info("I got a %s", r.status_code) if r.status_code == 200: try: return r.json() except ValueError: - print "returning text" return r.text @@ -79,7 +83,6 @@ def google_api_post(email, url, post_data, authorized=True): try: return r.json() except ValueError: - print "returning text" return r.text From 8af7c676fb8a3d8377f74150b69aadc60593164e Mon Sep 17 00:00:00 2001 From: Adam Coddington Date: Sun, 1 Jun 2014 14:26:19 -0700 Subject: [PATCH 3/4] Remoing unaccessed 'fetch_contacts' function. --- django_mailbox/google_utils.py | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/django_mailbox/google_utils.py b/django_mailbox/google_utils.py index 4d6b773..b834f59 100644 --- a/django_mailbox/google_utils.py +++ b/django_mailbox/google_utils.py @@ -109,25 +109,3 @@ def fetch_user_info(email): "https://www.googleapis.com/oauth2/v1/userinfo?alt=json" ) return result - - -def fetch_google_contacts(email, limit=10000): - result = google_api_get( - email, - "https://www.google.com/m8/feeds/contacts/default/full" - "?v=3.0&alt=json&max-results=%s" % limit - ) - entries = result['feed']['entry'] - valid_entries = [ - x for x in entries - if u'gd$email' in x.keys() and u'gd$name' in x.keys() - ] - contacts = [] - for each in valid_entries: - try: - name = each[u'gd$name'][u'gd$fullName'][u'$t'] - except KeyError: - name = None - for each_email in each[u'gd$email']: - contacts.append(dict(name=name, email=each_email[u'address'])) - return contacts From 652ae282ba28070221d2d9384049284fab239e1d Mon Sep 17 00:00:00 2001 From: Adam Coddington Date: Sun, 1 Jun 2014 14:27:03 -0700 Subject: [PATCH 4/4] Bumping version number to 3.4. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 1f3800b..447350a 100755 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ gmail_oauth2_require = [ setup( name='django-mailbox', - version='3.3', + version='3.4', url='http://github.com/latestrevision/django-mailbox/', description=( 'Import mail from POP3, IMAP, local mailboxes or directly from '