Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alex Tatay
committed
Apr 7, 2016
1 parent
e52c565
commit 3e440aa
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import json | ||
import logging | ||
from engineauth.models import User | ||
from engineauth.strategies.oauth2 import OAuth2Strategy | ||
|
||
|
||
class SinaWeiboStrategy(OAuth2Strategy): | ||
|
||
@property | ||
def options(self): | ||
return { | ||
'provider': 'sinaweibo', | ||
'token_uri': 'https://api.weibo.com/oauth2/access_token', | ||
'auth_uri': 'https://api.weibo.com/oauth2/authorize', | ||
} | ||
|
||
def user_info(self, req): | ||
# get user id | ||
url = 'https://api.weibo.com/2/account/get_uid.json?access_token={}'.format(req.credentials.access_token) | ||
res, results = self.http(req).request(url) | ||
if res.status != 200: | ||
return self.raise_error('There was an error contacting Sina Weibo. Please try again.') | ||
uid = json.loads(results)['uid'] | ||
# get user data | ||
url = 'https://api.weibo.com/2/users/show.json?uid={}&access_token={}'.format(uid, req.credentials.access_token) | ||
res, results = self.http(req).request(url) | ||
if res.status != 200: | ||
return self.raise_error('There was an error contacting Sina Weibo. Please try again.') | ||
user = json.loads(results) | ||
user_dict = { | ||
'auth_id': User.generate_auth_id(req.provider, user['id']), | ||
'uid': user['id'], | ||
'info': { | ||
'id': user.get('id'), | ||
'displayName': user.get('screen_name'), | ||
'name': { | ||
'formatted': user.get('name') | ||
}, | ||
'gender': user.get('gender'), | ||
'urls': [{ | ||
'value': user.get('url') | ||
}], | ||
'verified': user.get('verified'), | ||
'location': user.get('location'), | ||
'image': { | ||
'url': user.get('profile_image_url') | ||
} | ||
}, | ||
'extra': { | ||
'raw_info': user, | ||
} | ||
} | ||
return user_dict |