Skip to content

Commit

Permalink
Merge pull request #133 from cloverstd/master
Browse files Browse the repository at this point in the history
企业号 OAuth 示例
  • Loading branch information
messense committed Mar 13, 2016
2 parents 3db3c4d + 5d7c7b5 commit 833c918
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
62 changes: 61 additions & 1 deletion docs/oauth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,64 @@
微信 OAuth 网页授权
=========================

TODO
微信企业号 OAuth 网页授权接入
-------------------------

下述代码使用 Flask_. 框架作为示例

.. _Flask: http://flask.pocoo.org

.. code-block:: python
from flask import Flask, request, redirect, jsonify, session, abort
from wechatpy.enterprise import WeChatClient
import functools
app = Flask(__name__)
CORP_ID = 'wxc480d56d906bc121'
SECRET = '79BAUPuQ0zcytpz7f5vouAFPwnWDK0XePjKeWsY7Wo-cpAZvYYAy0OH-PH0-6OUN'
app.secret_key = 'key'
client = WeChatClient(
CORP_ID,
SECRET
)
def oauth(method):
@functools.wraps(method)
def warpper(*args, **kwargs):
code = request.args.get('code', None)
url = client.oauth.authorize_url(request.url)
if code:
try:
user_info = client.oauth.get_user_info(code)
except Exception as e:
print e.errmsg, e.errcode
# 这里需要处理请求里包含的 code 无效的情况
abort(403)
else:
session['user_info'] = user_info
else:
return redirect(url)
return method(*args, **kwargs)
return warpper
@app.route('/')
@oauth
def index():
user_info = session.get('user_info')
return jsonify(data=user_info)
if __name__ == '__main__':
app.run(
debug=True,
port=9000,
)
6 changes: 3 additions & 3 deletions wechatpy/enterprise/client/api/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def authorize_url(self, redirect_uri, state=None):
url_list = [
self.OAUTH_BASE_URL,
'?appid=',
self.corp_id,
self._client.corp_id,
'&redirect_uri=',
redirect_uri,
'&response_type=code&scope=snsapi_base',
]
if self.state:
url_list.extend(['&state=', self.state])
if state:
url_list.extend(['&state=', state])
url_list.append('#wechat_redirect')
return ''.join(url_list)

Expand Down

0 comments on commit 833c918

Please sign in to comment.