forked from yueyoum/social-oauth
-
Notifications
You must be signed in to change notification settings - Fork 9
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
1 parent
c2a2701
commit 3bc278d
Showing
6 changed files
with
185 additions
and
6 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,54 @@ | ||
# -*- coding: utf-8 -*- | ||
import re | ||
import json | ||
|
||
from socialoauth.sites.base import OAuth2 | ||
from socialoauth.exception import SocialAPIError | ||
|
||
|
||
QQ_OPENID_PATTERN = re.compile('\{.+\}') | ||
|
||
class QQApp(OAuth2): | ||
AUTHORIZE_URL = 'https://graph.qq.com/oauth2.0/authorize' | ||
ACCESS_TOKEN_URL = 'https://graph.qq.com/oauth2.0/token' | ||
|
||
OPENID_URL = 'https://graph.qq.com/oauth2.0/me' | ||
|
||
|
||
@property | ||
def authorize_url(self): | ||
url = super(QQ, self).authorize_url | ||
return '%s&state=socialoauth' % url | ||
|
||
|
||
def get_access_token(self, code): | ||
super(QQ, self).get_access_token(code, method='GET', parse=False) | ||
|
||
|
||
def build_api_url(self, url): | ||
return url | ||
|
||
def build_api_data(self, **kwargs): | ||
data = { | ||
'access_token': self.access_token, | ||
'oauth_consumer_key': self.CLIENT_ID, | ||
'openid': self.uid | ||
} | ||
data.update(kwargs) | ||
return data | ||
|
||
def parse_token_response(self, res): | ||
self.uid = res['userid'] | ||
self.access_token = res['access_token'] | ||
self.expires_in = None | ||
self.refresh_token = None | ||
|
||
_url = 'https://graph.qq.com/user/get_user_info' | ||
res = self.api_call_get(_url) | ||
if res['ret'] != 0: | ||
raise SocialAPIError(self.site_name, _url, res) | ||
|
||
self.name = res['nickname'] | ||
self.avatar = res['figureurl_qq_1'] | ||
self.avatar_large = res['figureurl_qq_2'] | ||
self.gender = res['gender'] |
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,80 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import json | ||
from urllib import quote_plus | ||
|
||
from socialoauth.sites.base import OAuth2 | ||
from socialoauth.exception import SocialAPIError, SocialSitesConfigError | ||
|
||
|
||
class WechatApp(OAuth2): | ||
AUTHORIZE_URL = 'https://open.weixin.qq.com/connect/oauth2/authorize' | ||
ACCESS_TOKEN_URL = 'https://api.weixin.qq.com/sns/oauth2/access_token' | ||
OPENID_URL = 'https://api.weixin.qq.com/sns/userinfo' | ||
|
||
SUPPORTED_SCOPES = ('snsapi_base', 'snsapi_userinfo') | ||
|
||
@property | ||
def authorize_url(self): | ||
url = "%s?appid=%s&redirect_uri=%s&response_type=code" % ( | ||
self.AUTHORIZE_URL, self.CLIENT_ID, quote_plus(self.REDIRECT_URI) | ||
) | ||
|
||
if getattr(self, 'SCOPE', None) is not None: | ||
if (self.SCOPE in self.SUPPORTED_SCOPES): | ||
url = '%s&scope=%s' % (url, self.SCOPE) | ||
else: | ||
raise SocialSitesConfigError("SCOPE must be one of (%s)." %(','.join(self.SUPPORTED_SCOPES)), None) | ||
else: | ||
raise SocialSitesConfigError("SCOPE is required!", None) | ||
|
||
url = url + '&state=socialoauth#wechat_redirect' | ||
return url | ||
|
||
def get_access_token(self, code): | ||
data = { | ||
'appid': self.CLIENT_ID, | ||
'secret': self.CLIENT_SECRET, | ||
'redirect_uri': self.REDIRECT_URI, | ||
'code': code, | ||
'grant_type': 'authorization_code' | ||
} | ||
|
||
res = self.http_get(self.ACCESS_TOKEN_URL, data, parse=False) | ||
self.parse_token_response(res) | ||
|
||
def build_api_url(self, url): | ||
return url | ||
|
||
def build_api_data(self, **kwargs): | ||
data = { | ||
'access_token': self.access_token, | ||
'openid': self.uid | ||
} | ||
data.update(kwargs) | ||
return data | ||
|
||
def parse_token_response(self, res): | ||
res = json.loads(res) | ||
|
||
self.access_token = res['access_token'] | ||
self.expires_in = int(res['expires_in']) | ||
self.refresh_token = res['refresh_token'] | ||
|
||
self.uid = res['openid'] | ||
self.unionid = res['unionid'] | ||
|
||
if self.SCOPE == 'snsapi_userinfo': | ||
res = self.api_call_get(self.OPENID_URL, lang='zh_CN') | ||
|
||
if 'errcode' in res: | ||
raise SocialAPIError(self.site_name, self.OPENID_URL, res) | ||
|
||
self.name = res['nickname'] | ||
self.avatar = res['headimgurl'] | ||
self.avatar_large = res['headimgurl'] | ||
self.gender = res['sex'] | ||
else: | ||
self.name = '' | ||
self.avatar = '' | ||
self.avatar_large = '' |
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,43 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from socialoauth.sites.base import OAuth2 | ||
|
||
|
||
class WeiboApp(OAuth2): | ||
AUTHORIZE_URL = 'https://api.weibo.com/oauth2/authorize' | ||
ACCESS_TOKEN_URL = 'https://api.weibo.com/oauth2/access_token' | ||
|
||
|
||
def build_api_url(self, url): | ||
return url | ||
|
||
def build_api_data(self, **kwargs): | ||
data = { | ||
'access_token': self.access_token | ||
} | ||
data.update(kwargs) | ||
return data | ||
|
||
def parse_token_response(self, res): | ||
self.uid = res['userid'] | ||
self.access_token = res['access_token'] | ||
self.expires_in = None | ||
self.refresh_token = None | ||
|
||
res = self.api_call_get( | ||
'https://api.weibo.com/2/users/show.json', | ||
uid=self.uid | ||
) | ||
|
||
self.name = res['name'] | ||
self.avatar = res['profile_image_url'] | ||
self.avatar_large = res['avatar_large'] | ||
self.gender = res['gender'] | ||
|
||
|
||
def post_status(self, text): | ||
if isinstance(text, unicode): | ||
text = text.encode('utf-8') | ||
|
||
url = 'https://api.weibo.com/2/statuses/update.json' | ||
res = self.api_call_post(url, status=text) |