Skip to content

Commit

Permalink
添加应用
Browse files Browse the repository at this point in the history
  • Loading branch information
gusibi committed Mar 16, 2015
0 parents commit 827d5d0
Show file tree
Hide file tree
Showing 28 changed files with 814 additions and 0 deletions.
15 changes: 15 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BSD License

For python-instagram software

Copyright (c) 2014, Facebook, Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

Neither the name Facebook nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
python-weixin
-----
A Python client for the Weixin REST APIs

Installation
-----
python setup.py install

Requires
-----
* requests
* simplejson
* six


Authentication
-----
Weixin API uses the OAuth2 protocol for authentication, but not all functionality requires authentication.
See the docs for more information: https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN


### Authenticating a user
(TODO)The provided sample app shows a simple OAuth flow for authenticating a user and getting an access token for them.


### Using an access token
Once you have an access token (whether via the script or from the user flow), you can pass that token into the WeixinAPI constructor:

``` python
from weixin.client import WeixinAPI

scope = ("snsapi_login", )
api = WeixinAPI(appid=APP_ID,
app_secret=APP_SECRET,
redirect_uri=REDIRECT_URI)
authorize_url = api.get_authorize_url(scope=scope)

access_token = api.exchange_code_for_access_token(code=code)

api = WeixinAPI(access_token=access_token)

user = api.user(openid="openid")
```

Binary file added dist/python_weixin-0.0.1-py2.7.egg
Binary file not shown.
38 changes: 38 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-


from weixin.client import WeixinAPI
from weixin.oauth2 import OAuth2AuthExchangeError

APP_ID = 'wxbdc5610cc59c1631'
APP_SECRET = 'your app secret'
REDIRECT_URI = 'https://passport.yhd.com/wechat/callback.do'


code = '021b66a31b7179822b01e7e2c12528cV'

api = WeixinAPI(appid=APP_ID,
app_secret=APP_SECRET,
redirect_uri=REDIRECT_URI)

try:
print api.get_authorize_login_url(scope=("snsapi_login",))
print api.exchange_code_for_access_token(code=code)
except OAuth2AuthExchangeError, e:
print e

auth_info = {
'access_token': 'OezXcEiiBSKSxW0eoylIeGXVgVFIUy2pK5I7TVatC5MGtVqTIWjtyV5Pax8ZLiWw-NdEN9dPkEX8Yewsve2AktmzS0gmbvzRKO49l6sxHRfhXg1no5ObdGufYhRIubP2m3FUdv-Cop3t3S_xwMbBWQ',
'refresh_token': 'OezXcEiiBSKSxW0eoylIeGXVgVFIUy2pK5I7TVatC5MGtVqTIWjtyV5Pax8ZLiWw44bjXRXdmPsclqGIjWs777H3p00QI9a3hzX265Uq9fPJZttNQApdCRPbySXDfofbjniiwsVJiT7fTv7j5jCAxg',
'openid': u'oV02tuA8Wt6Kk7S0pVydThYvmSJA',
'expires_in': 7200,
'scope': u'snsapi_login'}

print api.exchange_refresh_token_for_access_token(refresh_token=auth_info['refresh_token'])

api = WeixinAPI(access_token=auth_info['access_token'])

r = api.user(openid=auth_info['openid'])
print r
v = api.validate_token(openid=auth_info['openid'])
print v
52 changes: 52 additions & 0 deletions get_access_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-

import sys

from weixin.client import WeixinAPI
from weixin.oauth2 import OAuth2AuthExchangeError

if len(sys.argv) > 1 and sys.argv[1] == 'local':
try:
from test_settings import *

WeixinAPI.host = test_host
WeixinAPI.base_path = test_base_path
WeixinAPI.access_token_field = "access_token"
WeixinAPI.authorize_url = test_authorize_url
WeixinAPI.access_token_url = test_access_token_url
WeixinAPI.protocol = test_protocol
except Exception:
pass


# Fix Python 2.x.
try:
import __builtin__
input = getattr(__builtin__, 'raw_input')
except (ImportError, AttributeError):
pass


appid = input("App ID: ").strip()
app_secret = input("App Secret: ").strip()
redirect_uri = input("Redirect URI: ").strip()
raw_scope = input("Requested scope (separated by spaces, blank for just basic read): ").strip()
scope = raw_scope.split(' ')
# For basic, API seems to need to be set explicitly
if not scope or scope == [""]:
scope = ["snsapi_login"]


api = WeixinAPI(appid=appid,
app_secret=app_secret,
redirect_uri=redirect_uri)
redirect_uri = api.get_authorize_login_url(scope=scope)

print ("Visit this page and authorize access in your browser: "+ redirect_uri)

code = (str(input("Paste in code in query string after redirect: ").strip()))

access_token = api.exchange_code_for_access_token(code)

print ("access token: " )
print (access_token)
11 changes: 11 additions & 0 deletions python_weixin.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Metadata-Version: 1.0
Name: python-weixin
Version: 0.0.1
Summary: Weixin API client
Home-page: https://github.com/zongxiao/python-weixin
Author: Zongxiao Cheng
Author-email: cacique1103@gmail.com
License: GPL
Description: UNKNOWN
Keywords: weixin
Platform: UNKNOWN
13 changes: 13 additions & 0 deletions python_weixin.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
setup.py
python_weixin.egg-info/PKG-INFO
python_weixin.egg-info/SOURCES.txt
python_weixin.egg-info/dependency_links.txt
python_weixin.egg-info/requires.txt
python_weixin.egg-info/top_level.txt
python_weixin.egg-info/zip-safe
weixin/__init__.py
weixin/bind.py
weixin/client.py
weixin/helper.py
weixin/json_import.py
weixin/oauth2.py
1 change: 1 addition & 0 deletions python_weixin.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

4 changes: 4 additions & 0 deletions python_weixin.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
simplejson
requests
six
chardet
1 change: 1 addition & 0 deletions python_weixin.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
weixin
1 change: 1 addition & 0 deletions python_weixin.egg-info/zip-safe
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
simplejson==3.6.3
requests=2.4.1
chardet=2.3.0
six==1.8.0
43 changes: 43 additions & 0 deletions sample_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from flask import Flask
from flask import Markup
from flask import redirect
from flask import request
from flask import jsonify

from weixin.client import WeixinAPI
from weixin.oauth2 import OAuth2AuthExchangeError

app = Flask(__name__)

APP_ID = 'test'
APP_SECRET = 'test'
REDIRECT_URI = 'http://localhost.com/authorization'


@app.route("/authorization")
def authorization():
code = request.args.get('code')
api = WeixinAPI(appid=APP_ID,
app_secret=APP_SECRET,
redirect_uri=REDIRECT_URI)
auth_info = api.exchange_code_for_access_token(code=code)
api = WeixinAPI(access_token=auth_info['access_token'])
resp = api.user(openid=auth_info['openid'])
return jsonify(resp)


@app.route("/login")
def login():
api = WeixinAPI(appid=APP_ID,
app_secret=APP_SECRET,
redirect_uri=REDIRECT_URI)
redirect_uri = api.get_authorize_login_url(scope=("snsapi_login",))
return redirect(redirect_uri)


@app.route("/")
def hello():
return Markup('<a href="%s">weixin login!</a>') % '/login'

if __name__ == "__main__":
app.run(debug=True)
14 changes: 14 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env python
from setuptools import setup, find_packages

setup(name="python-weixin",
version="0.0.1",
description="Weixin API client",
license="BSD",
install_requires=["simplejson","requests","six", "chardet"],
author="Zongxiao Cheng",
author_email="cacique1103@gmail.com",
url="https://github.com/zongxiao/python-weixin",
packages = find_packages(),
keywords= "weixin",
zip_safe = True)
38 changes: 38 additions & 0 deletions test_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- coding: utf-8 -*-


from weixin.client import WeixinAPI
from weixin.oauth2 import OAuth2AuthExchangeError

APP_ID = 'your app id'
APP_SECRET = 'your secret'
REDIRECT_URI = 'http://www.exmple.com'


code = '0418f2c46cd26e4f9eee5bf03320662M'

api = WeixinAPI(appid=APP_ID,
app_secret=APP_SECRET,
redirect_uri=REDIRECT_URI)

try:
pass
# print api.get_authorize_login_url(scope=("snsapi_login",))
# print api.exchange_code_for_access_token(code=code)
except OAuth2AuthExchangeError, e:
print e


auth_info = {
'access_token': 'OezXcEiiBSKSxW0eoylIeGXVgVFIUy2pK5I7TVatC5MGtVqTIWjtyV5Pax8ZLiWw-NdEN9dPkEX8Yewsve2AktmzS0gmbvzRKO49l6sxHRfhXg1no5ObdGufYhRIubP2m3FUdv-Cop3t3S_xwMbBWQ',
'refresh_token': 'OezXcEiiBSKSxW0eoylIeGXVgVFIUy2pK5I7TVatC5MGtVqTIWjtyV5Pax8ZLiWw44bjXRXdmPsclqGIjWs777H3p00QI9a3hzX265Uq9fPJZttNQApdCRPbySXDfofbjniiwsVJiT7fTv7j5jCAxg',
'openid': u'oV02tuA8Wt6Kk7S0pVydThYvmSJA',
'expires_in': 7200,
'scope': u'snsapi_login'}

print api.exchange_refresh_token_for_access_token(refresh_token=auth_info['refresh_token'])
api = WeixinAPI(access_token=auth_info['access_token'])
r = api.user(openid=auth_info['openid'])
print r
v = api.validate_token(openid=auth_info['openid'])
print v
5 changes: 5 additions & 0 deletions test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test_host = "open.weixin.qq.com"
test_base_path = ""
test_authorize_url = "https://open.weixin.qq.com/connect/qrconnect"
test_access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token"
test_protocol = "https"
10 changes: 10 additions & 0 deletions weixin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-

__title__ = 'requests'
__version__ = '0.0.1'
__author__ = 'Zongxiao Cheng'
__license__ = 'BSD'


from .bind import WeixinClientError, WeixinAPIError
from .client import WeixinAPI
Binary file added weixin/__init__.pyc
Binary file not shown.
Loading

0 comments on commit 827d5d0

Please sign in to comment.