Skip to main content

Wechat Oauth Module for Python.

Project description

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)
      

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pywe-oauth-1.1.2.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

pywe_oauth-1.1.2-py2.py3-none-any.whl (4.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pywe-oauth-1.1.2.tar.gz.

File metadata

  • Download URL: pywe-oauth-1.1.2.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for pywe-oauth-1.1.2.tar.gz
Algorithm Hash digest
SHA256 b67199a1056cc691c1b669a8d732076973870ef83eb8c7ec805b3e6e06763d55
MD5 3d137fe31cfbf2606cdf2c698875610d
BLAKE2b-256 2b2a3c4dc68ce043c2ec5a88cae7228ed2011ae750e0de8e9d4d2e34697de1ab

See more details on using hashes here.

File details

Details for the file pywe_oauth-1.1.2-py2.py3-none-any.whl.

File metadata

  • Download URL: pywe_oauth-1.1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for pywe_oauth-1.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 918f6c060de7866bf4ece9806c5504161a38b475ed7c37eeccb21a9bc53e62c7
MD5 11adff8a3cbdd178b9e6fee23d75446e
BLAKE2b-256 0e17606862b21513460d2a1ff8277a21fe9547391375cd98c144a5b5c9f7f5c8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page