[Python-modules-commits] [python-social-auth] 143/322: Fix Issue #532, get UID when use access_token ajax auth in weibo backends.

Wolfgang Borgert debacle at moszumanska.debian.org
Sat Dec 24 15:13:01 UTC 2016


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

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

commit c33c94761e7cf5ac5936abf5d7f16ce74d627f61
Author: zz <zz.at.field at gmail.com>
Date:   Tue Feb 24 20:18:44 2015 +0800

    Fix Issue #532, get UID when use access_token ajax auth in weibo backends.
---
 social/backends/weibo.py | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/social/backends/weibo.py b/social/backends/weibo.py
index 6082ae9..0bda1b5 100644
--- a/social/backends/weibo.py
+++ b/social/backends/weibo.py
@@ -5,7 +5,7 @@ Weibo OAuth2 backend, docs at:
     http://psa.matiasaguirre.net/docs/backends/weibo.html
 """
 from social.backends.oauth import BaseOAuth2
-
+import json
 
 class WeiboOAuth2(BaseOAuth2):
     """Weibo (of sina) OAuth authentication backend"""
@@ -14,6 +14,7 @@ class WeiboOAuth2(BaseOAuth2):
     AUTHORIZATION_URL = 'https://api.weibo.com/oauth2/authorize'
     REQUEST_TOKEN_URL = 'https://api.weibo.com/oauth2/request_token'
     ACCESS_TOKEN_URL = 'https://api.weibo.com/oauth2/access_token'
+    ACCESS_TOKEN_INFO_URL = 'https://api.weibo.com/oauth2/get_token_info'
     ACCESS_TOKEN_METHOD = 'POST'
     REDIRECT_STATE = False
     EXTRA_DATA = [
@@ -39,7 +40,28 @@ class WeiboOAuth2(BaseOAuth2):
                 'first_name': first_name,
                 'last_name': last_name}
 
+    def get_uid(self, access_token):
+        """ return uid by access_token"""
+
+        response = self.request(self.ACCESS_TOKEN_INFO_URL,
+                                method='POST',
+                                params={'access_token':access_token})
+
+        data = response.json()
+        return data['uid']
+
     def user_data(self, access_token, *args, **kwargs):
-        return self.get_json('https://api.weibo.com/2/users/show.json',
-                             params={'access_token': access_token,
-                                     'uid': kwargs['response']['uid']})
+        """ if use access_token for ajax auth, then would raise KeyError
+        because there is no uid in response, so must get uid.
+        """
+        if 'response' not in kwargs or 'uid' not in kwargs['response']:
+            uid = self.get_uid(access_token)
+            response = kwargs.setdefault('response', {})
+            response['uid'] = uid
+
+        response = self.get_json('https://api.weibo.com/2/users/show.json',
+                                params={'access_token': access_token,
+                                        'uid': kwargs['response']['uid']})
+
+        response['uid'] = kwargs['response']['uid']
+        return response
\ No newline at end of file

-- 
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