diff --git a/README.rst b/README.rst index f2b051689..08c001338 100644 --- a/README.rst +++ b/README.rst @@ -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 @@ -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 diff --git a/docs/backends/mailchimp.rst b/docs/backends/mailchimp.rst new file mode 100644 index 000000000..e27fc8139 --- /dev/null +++ b/docs/backends/mailchimp.rst @@ -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/ diff --git a/social/backends/mailchimp.py b/social/backends/mailchimp.py new file mode 100644 index 000000000..6c791ac28 --- /dev/null +++ b/social/backends/mailchimp.py @@ -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 + })