pywe-oauth
Wechat Oauth Module for Python.
Installation
pip install pywe-oauth
Usage
from pywe_oauth import get_access_info, get_oauth_code_url, get_userinfo, get_component_access_info, get_component_oauth_code_url, get_component_userinfo, get_oauth_redirect_url
Method
def get_oauth_code_url(self, appid=None, redirect_uri=None, scope='snsapi_base', redirect_url=None, component=False, component_appid=None):
def get_access_info(self, appid=None, secret=None, code=None, component=False, component_appid=None, component_access_token=None):
def get_userinfo(self, access_token=None, openid=None, component=False):
def get_component_oauth_code_url(self, appid=None, redirect_uri=None, scope='snsapi_base', redirect_url=None, component_appid=None):
def get_component_access_info(self, appid=None, code=None, component_appid=None, component_access_token=None):
def get_component_userinfo(self, access_token=None, openid=None):
def get_oauth_redirect_url(self, oauth_uri, scope='snsapi_base', redirect_url=None, default_url=None, direct_redirect=None, random_str=True):
Relative Libs
OAuth2 URL Examples
https://wx.com/oauth2?redirect_url=redirect_url
https://wx.com/oauth2?redirect_url=redirect_url&default_url=default_url
https://wx.com/oauth2?scope=snsapi_base&redirect_url=redirect_url
https://wx.com/oauth2?scope=snsapi_base&redirect_url=redirect_url&default_url=default_url
https://wx.com/oauth2?scope=snsapi_base&redirect_url=redirect_url&default_url=default_url&direct_redirect=true
Backend Examples
-
Django
-
settings.py
# Wechat Settings WECHAT = { 'JSAPI': { 'token': '5201314', 'appID': '', 'appsecret': '', 'mchID': '', 'apiKey': '', 'mch_cert': '', 'mch_key': '', 'redpack': { 'SEND_NAME': '', 'NICK_NAME': '', 'ACT_NAME': '', 'WISHING': '', 'REMARK': '', } }, } WECHAT_OAUTH2_REDIRECT_URI = 'https://wx.com/wx_oauth2?scope={}&redirect_url={}' WECHAT_BASE_REDIRECT_URI = 'https://wx.com/base_redirect' WECHAT_USERINFO_REDIRECT_URI = 'https://wx.com/userinfo_redirect'
-
urls.py
# -*- coding: utf-8 -*- from django.conf.urls import include, url from wechat import views as wx_views urlpatterns = [ url(r'^oauth2$', wx_views.wx_oauth2, name='wx_oauth2'), url(r'^base_redirect$', wx_views.base_redirect, name='base_redirect'), url(r'^userinfo_redirect$', wx_views.userinfo_redirect, name='userinfo_redirect'), ]
-
views.py
# -*- coding: utf-8 -*- from django.conf import settings from django.shortcuts import redirect from furl import furl from pywe_oauth import get_access_info, get_oauth_code_url, get_oauth_redirect_url, get_userinfo JSAPI = settings.WECHAT.get('JSAPI', {}) def wx_oauth2(request): scope = request.GET.get('scope', 'snsapi_userinfo') redirect_url = request.GET.get('redirect_url', '') default_url = request.GET.get('default_url', '') if request.weixin: redirect_uri = settings.WECHAT_USERINFO_REDIRECT_URI if scope == 'snsapi_userinfo' else settings.WECHAT_BASE_REDIRECT_URI return redirect(get_oauth_code_url(JSAPI['appID'], redirect_uri, scope, redirect_url)) return redirect(default_url or redirect_url) def base_redirect(request): """ snsapi_base cannot get unionid and userinfo """ code = request.GET.get('code', '') state = request.GET.get('state', '') access_info = get_access_info(JSAPI['appID'], JSAPI['appsecret'], code) if 'errcode' in access_info: return redirect(get_oauth_redirect_url(settings.WECHAT_OAUTH2_REDIRECT_URI, 'snsapi_base', state)) return redirect(furl(state).add(access_info).url) def userinfo_redirect(request): code = request.GET.get('code', '') state = request.GET.get('state', '') access_info = get_access_info(JSAPI['appID'], JSAPI['appsecret'], code) if 'errcode' in access_info: return redirect(get_oauth_redirect_url(settings.WECHAT_OAUTH2_REDIRECT_URI, 'snsapi_userinfo', state)) userinfo = get_userinfo(access_info.get('access_token', ''), access_info.get('openid', '')) if 'openid' not in userinfo: return redirect(get_oauth_redirect_url(settings.WECHAT_OAUTH2_REDIRECT_URI, 'snsapi_userinfo', state)) # Save Userinfo Or Other Handle # Some codes return redirect(furl(state).add(userinfo).url)
-
Metadata
Release files for pywe-oauth 1.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pywe-oauth-1.1.2.tar.gz | 3.9 kB | Without provenance | Details for pywe-oauth-1.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pywe_oauth-1.1.2-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for pywe_oauth-1.1.2-py2.py3-none-any.whl |
Total release size: 8.2 kB
Release files / pywe-oauth-1.1.2.tar.gz
| Download URL | pywe-oauth-1.1.2.tar.gz |
|---|---|
| Size | 3.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b67199a1056cc691c1b669a8d732076973870ef83eb8c7ec805b3e6e06763d55
|
|
BLAKE2b-256 checksum How to use checksums |
2b2a3c4dc68ce043c2ec5a88cae7228ed2011ae750e0de8e9d4d2e34697de1ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.1
|
Release files / pywe_oauth-1.1.2-py2.py3-none-any.whl
| Download URL | pywe_oauth-1.1.2-py2.py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
918f6c060de7866bf4ece9806c5504161a38b475ed7c37eeccb21a9bc53e62c7
|
|
BLAKE2b-256 checksum How to use checksums |
0e17606862b21513460d2a1ff8277a21fe9547391375cd98c144a5b5c9f7f5c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.1
|
Security for pywe-oauth 1.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.