Skip to content

Commit

Permalink
add qq_app weibo_app, wechat_app
Browse files Browse the repository at this point in the history
  • Loading branch information
seasonstar committed Dec 10, 2015
1 parent c2a2701 commit 3bc278d
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 6 deletions.
2 changes: 1 addition & 1 deletion socialoauth/sites/qq.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ def parse_token_response(self, res):
self.name = res['nickname']
self.avatar = res['figureurl_qq_1']
self.avatar_large = res['figureurl_qq_2']

self.gender = res['gender']
54 changes: 54 additions & 0 deletions socialoauth/sites/qq_app.py
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']
8 changes: 5 additions & 3 deletions socialoauth/sites/wechat.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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)
Expand Down Expand Up @@ -62,16 +62,18 @@ def parse_token_response(self, res):
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 = ''
Expand Down
80 changes: 80 additions & 0 deletions socialoauth/sites/wechat_app.py
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 = ''
4 changes: 2 additions & 2 deletions socialoauth/sites/weibo.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def build_api_data(self, **kwargs):
def parse_token_response(self, res):
self.uid = res['uid']
self.access_token = res['access_token']
self.expires_in = res['expires_in']
self.expires_in = None
self.refresh_token = None

res = self.api_call_get(
Expand All @@ -32,6 +32,7 @@ def parse_token_response(self, res):
self.name = res['name']
self.avatar = res['profile_image_url']
self.avatar_large = res['avatar_large']
self.gender = res['gender']



Expand All @@ -41,4 +42,3 @@ def post_status(self, text):

url = 'https://api.weibo.com/2/statuses/update.json'
res = self.api_call_post(url, status=text)

43 changes: 43 additions & 0 deletions socialoauth/sites/weibo_app.py
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)

0 comments on commit 3bc278d

Please sign in to comment.