Skip to content

Commit

Permalink
[NEW]Add WeChatOAuth qrconnect_url property
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed May 3, 2015
1 parent b20e72d commit 2297efe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 7 additions & 0 deletions tests/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ def test_get_authorize_url(self):
authorize_url
)

def test_get_qrconnect_url(self):
url = self.oauth.qrconnect_url
self.assertEqual(
'https://open.weixin.qq.com/connect/qrconnect?appid=123456&redirect_uri=http%3A//localhost&response_type=code&scope=snsapi_login#wechat_redirect', # NOQA
url
)

def test_fetch_access_token(self):
with HTTMock(wechat_api_mock):
res = self.oauth.fetch_access_token('123456')
Expand Down
24 changes: 22 additions & 2 deletions wechatpy/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class WeChatOAuth(object):
""" 微信公众平台 OAuth 网页授权 """

API_BASE_URL = 'https://api.weixin.qq.com/'
OAUTH_BASE_URL = 'https://open.weixin.qq.com/connect/oauth2/'
OAUTH_BASE_URL = 'https://open.weixin.qq.com/connect/'

def __init__(self, app_id, secret, redirect_uri,
scope='snsapi_base', state=''):
Expand Down Expand Up @@ -82,7 +82,7 @@ def authorize_url(self):
redirect_uri = six.moves.urllib.parse.quote(self.redirect_uri)
url_list = [
self.OAUTH_BASE_URL,
'authorize?appid=',
'oauth2/authorize?appid=',
self.app_id,
'&redirect_uri=',
redirect_uri,
Expand All @@ -94,6 +94,26 @@ def authorize_url(self):
url_list.append('#wechat_redirect')
return ''.join(url_list)

@property
def qrconnect_url(self):
"""Generate qrconnect url
:return: An url
"""
redirect_uri = six.moves.urllib.parse.quote(self.redirect_uri)
url_list = [
self.OAUTH_BASE_URL,
'qrconnect?appid=',
self.app_id,
'&redirect_uri=',
redirect_uri,
'&response_type=code&scope=',
'snsapi_login' # scope
]
if self.state:
url_list.extend(['&state=', self.state])
url_list.append('#wechat_redirect')
return ''.join(url_list)

def fetch_access_token(self, code):
"""Fetch OAuth2 access token
:param code: code argument from url
Expand Down

0 comments on commit 2297efe

Please sign in to comment.