[Python-modules-commits] [python-social-auth] 04/89: Add pinterest backend

Wolfgang Borgert debacle at moszumanska.debian.org
Sat Dec 24 15:14:50 UTC 2016


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to tag v0.2.15
in repository python-social-auth.

commit 54a7bacc29be6e2aa4595b104ea91b8be123be5f
Author: Vlasov Dmitriy <scailer at veles.biz>
Date:   Mon Oct 26 17:09:07 2015 +0500

    Add pinterest backend
---
 README.rst                              |  2 ++
 docs/backends/index.rst                 |  1 +
 docs/backends/pinterest.rst             | 29 +++++++++++++++++++
 docs/intro.rst                          |  2 ++
 social/backends/pinterest.py            | 51 +++++++++++++++++++++++++++++++++
 social/tests/backends/test_pinterest.py | 49 +++++++++++++++++++++++++++++++
 6 files changed, 134 insertions(+)

diff --git a/README.rst b/README.rst
index 03df9cd..c15752d 100644
--- a/README.rst
+++ b/README.rst
@@ -97,6 +97,7 @@ or current ones extended):
     * OpenId_
     * OpenStreetMap_ OAuth1 http://wiki.openstreetmap.org/wiki/OAuth
     * OpenSuse_ OpenId http://en.opensuse.org/openSUSE:Connect
+    * Pinterest_ OAuth2
     * PixelPin_ OAuth2
     * Pocket_ OAuth2
     * Podio_ OAuth2
@@ -317,3 +318,4 @@ check `django-social-auth LICENSE`_ for details:
 .. _requests: http://docs.python-requests.org/en/latest/
 .. _PixelPin: http://pixelpin.co.uk
 .. _Zotero: http://www.zotero.org/
+.. _Pinterest: https://www.pinterest.com
diff --git a/docs/backends/index.rst b/docs/backends/index.rst
index a6b63da..2734078 100644
--- a/docs/backends/index.rst
+++ b/docs/backends/index.rst
@@ -103,6 +103,7 @@ Social backends
    openstreetmap
    orbi
    persona
+   pinterest
    pixelpin
    pocket
    podio
diff --git a/docs/backends/pinterest.rst b/docs/backends/pinterest.rst
new file mode 100644
index 0000000..d55be1c
--- /dev/null
+++ b/docs/backends/pinterest.rst
@@ -0,0 +1,29 @@
+Pinterest
+=========
+
+Pinterest implemented OAuth2 protocol for their authentication mechanism. 
+To enable ``python-social-auth`` support follow this steps:
+
+1. Go to `Pinterest developers zone`_ and create an application.
+
+2. Fill App Id and Secret in your project settings::
+
+    SOCIAL_AUTH_PINTEREST_KEY = '...'
+    SOCIAL_AUTH_PINTEREST_SECRET = '...'
+    SOCIAL_AUTH_PINTEREST_SCOPE = [
+        'read_public', 
+        'write_public', 
+        'read_relationships', 
+        'write_relationships'
+    ]
+
+3. Enable the backend::
+
+    SOCIAL_AUTH_AUTHENTICATION_BACKENDS = (
+        ...
+        'social.backends.pinterest.PinterestOAuth2',
+        ...
+    )
+
+.. _Pinterest Apps Console: https://developers.pinterest.com/apps/
+.. _Pinterest Documentation: https://developers.pinterest.com/docs/ 
diff --git a/docs/intro.rst b/docs/intro.rst
index b138974..4f1aa77 100644
--- a/docs/intro.rst
+++ b/docs/intro.rst
@@ -64,6 +64,7 @@ or extend current one):
     * Odnoklassniki_ OAuth2 and Application Auth
     * OpenId_
     * Podio_ OAuth2
+    * Pinterest_ OAuth2
     * Rdio_ OAuth1 and OAuth2
     * Readability_ OAuth1
     * Shopify_ OAuth2
@@ -161,6 +162,7 @@ section.
 .. _Yahoo: http://yahoo.com
 .. _Yammer: https://www.yammer.com
 .. _Yandex: https://yandex.ru
+.. _Pinterest: https://www.pinterest.com
 .. _Readability: http://www.readability.com/
 .. _Stackoverflow: http://stackoverflow.com/
 .. _Steam: http://steamcommunity.com/
diff --git a/social/backends/pinterest.py b/social/backends/pinterest.py
new file mode 100644
index 0000000..3a90703
--- /dev/null
+++ b/social/backends/pinterest.py
@@ -0,0 +1,51 @@
+# -*- coding: utf-8 -*-
+"""
+Pinterest OAuth2 backend, docs at:
+    https://developers.pinterest.com/docs/api/authentication/
+"""
+
+from __future__ import unicode_literals
+
+import ssl
+
+from social.backends.oauth import BaseOAuth2
+
+
+class PinterestOAuth2(BaseOAuth2):
+    name = 'pinterest'
+    ID_KEY = 'user_id'
+    AUTHORIZATION_URL = 'https://api.pinterest.com/oauth/'
+    ACCESS_TOKEN_URL = 'https://api.pinterest.com/v1/oauth/token'
+    REDIRECT_STATE = False
+    ACCESS_TOKEN_METHOD = 'POST'
+    SSL_PROTOCOL = ssl.PROTOCOL_TLSv1
+    FAKE_HTTPS = False
+
+    def get_redirect_uri(self, state=None):
+        url = super(PinterestOAuth2, self).get_redirect_uri(state)
+        return self.FAKE_HTTPS and url.replace('http:', 'https:') or url
+
+    def user_data(self, access_token, *args, **kwargs):
+        response = self.get_json('https://api.pinterest.com/v1/me/',
+                                 params={'access_token': access_token})
+
+        if 'data' in response:
+            username = response['data']['url'].strip('/').split('/')[-1]
+            response = {
+                'user_id': response['data']['id'],
+                'first_name': response['data']['first_name'],
+                'last_name': response['data']['last_name'],
+                'username': username,
+            }
+        return response
+
+    def get_user_details(self, response):
+        fullname, first_name, last_name = self.get_user_names(
+            first_name=response['first_name'],
+            last_name=response['last_name'])
+
+        return {'username': response.get('username'),
+                'email': None,
+                'fullname': fullname,
+                'first_name': first_name,
+                'last_name': last_name}
diff --git a/social/tests/backends/test_pinterest.py b/social/tests/backends/test_pinterest.py
new file mode 100644
index 0000000..8f61e5d
--- /dev/null
+++ b/social/tests/backends/test_pinterest.py
@@ -0,0 +1,49 @@
+import json
+
+from social.tests.backends.oauth import OAuth2Test
+
+
+class PinterestOAuth2Test(OAuth2Test):
+    backend_path = 'social.backends.pinterest.PinterestOAuth2'
+    user_data_url = 'https://api.pinterest.com/v1/me/'
+    expected_username = 'foobar'
+    access_token_body = json.dumps({
+        'access_token': 'foobar',
+        'token_type': 'bearer'
+    })
+    user_data_body = json.dumps({
+        'id': '4788400174839062',
+        'first_name': 'Foo',
+        'last_name': 'Bar',
+        'username': 'foobar',
+    })
+
+    def test_login(self):
+        self.do_login()
+
+    def test_partial_pipeline(self):
+        self.do_partial_pipeline()
+
+
+class PinterestOAuth2BrokenServerResponseTest(OAuth2Test):
+    backend_path = 'social.backends.pinterest.PinterestOAuth2'
+    user_data_url = 'https://api.pinterest.com/v1/me/'
+    expected_username = 'foobar'
+    access_token_body = json.dumps({
+        'access_token': 'foobar',
+        'token_type': 'bearer'
+    })
+    user_data_body = json.dumps({
+        'data': {
+            'id': '4788400174839062',
+            'first_name': 'Foo',
+            'last_name': 'Bar',
+            'url': 'https://www.pinterest.com/foobar/',
+        }
+    })
+
+    def test_login(self):
+        self.do_login()
+
+    def test_partial_pipeline(self):
+        self.do_partial_pipeline()

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-social-auth.git



More information about the Python-modules-commits mailing list