Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MailChimp as an OAuth v2 backend #1037

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ or current ones extended):
* Live_ OAuth2
* Livejournal_ OpenId
* LoginRadius_ OAuth2 and Application Auth
* MailChimp_ OAuth2
* Mailru_ OAuth2
* MapMyFitness_ OAuth2
* Mendeley_ OAuth1 http://mendeley.com
Expand Down Expand Up @@ -268,6 +269,7 @@ check `django-social-auth LICENSE`_ for details:
.. _Live: https://live.com
.. _Livejournal: http://livejournal.com
.. _Khan Academy: https://github.com/Khan/khan-api/wiki/Khan-Academy-API-Authentication
.. _MailChimp: https://apidocs.mailchimp.com/oauth2/
.. _Mailru: https://mail.ru
.. _MapMyFitness: http://www.mapmyfitness.com/
.. _Mixcloud: https://www.mixcloud.com
Expand Down
25 changes: 25 additions & 0 deletions docs/backends/mailchimp.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
MailChimp
=======

MailChimp uses OAuth v2 for Authentication, check the `official docs`_.

- Create an app by filling out the form here: `Add App`_

- Fill ``Client ID`` and ``Client Secret`` values in the settings::

SOCIAL_AUTH_MAILCHIMP_KEY = '<App UID>'
SOCIAL_AUTH_MAILCHIMP_SECRET = '<App secret>'

- Add the backend to the ``AUTHENTICATION_BACKENDS`` setting::

AUTHENTICATION_BACKENDS = (
...
'social.backends.mailchimp.MailChimpOAuth2',
...
)

- Then you can start using ``{% url social:begin 'mailchimp' %}`` in
your templates

.. _official docs: https://apidocs.mailchimp.com/oauth2/
.. _Add App: https://admin.mailchimp.com/account/oauth2/
33 changes: 33 additions & 0 deletions social/backends/mailchimp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from social.backends.oauth import BaseOAuth2


class MailChimpOAuth2(BaseOAuth2):
"""MailChimp OAuth2 authentication backend"""
name = 'mailchimp'
AUTHORIZATION_URL = 'https://login.mailchimp.com/oauth2/authorize'
ACCESS_TOKEN_URL = 'https://login.mailchimp.com/oauth2/token'
METADATA_URL = 'https://login.mailchimp.com/oauth2/metadata'
ACCESS_TOKEN_METHOD = 'POST'
STATE_PARAMETER = False
REDIRECT_STATE = False
ID_KEY = 'user_id'
EXTRA_DATA = [
('accountname', 'accountname'),
('api_endpoint', 'api_endpoint'),
('role', 'role'),
('login', 'login')
]

def get_user_details(self, response):
"""Return user details from a Mailchimp metadata response"""
return {
'username': response['login']['login_name'],
'email': response['login']['email']
}

def user_data(self, access_token, *args, **kwargs):
"""Loads user data and datacenter information from service"""
return self.get_json(self.METADATA_URL,
headers={
'Authorization': 'OAuth ' + access_token
})