diff --git a/django_mailbox/google_utils.py b/django_mailbox/google_utils.py index a668ff6..b834f59 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 @@ -106,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 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 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 '