網站微信登陸-python 實現

最近微信登陸開放公測,爲了方便微信用戶使用,咱們的產品也決定加上微信登陸功能,而後就有了這篇筆記。python

根據需求選擇相應的登陸方式git

微信如今提供兩種登陸接入方式github

  • 移動應用微信登陸
  • 網站應用微信登陸

這裏咱們使用的是網站應用微信登陸web

按照 官方流程json

1 註冊並經過開放平臺開發者資質認證

註冊微信開放平臺賬號後,在賬號中心中填寫開發者資質認證申請,並等待認證經過。flask

2 建立網站應用

經過填寫網站應用名稱、簡介和圖標,以及各平臺下載地址等資料,建立網站應用api

3 接入微信登陸

在資源中心查閱網站應用開發文檔,開發接入微信登錄功能,讓用戶可以使用微信登陸你的網站應用瀏覽器

 

若是已經完成上面的操做,請繼續往下看微信

 

微信網站應用微信登陸是基於OAuth2.0協議標準構建的微信OAuth2.0受權登陸系統。app

微信OAuth2.0受權登陸目前支持authorization_code模式,適用於擁有server端的應用受權。該模式總體流程爲:

  1.  第三方發起微信受權登陸請求,微信用戶容許受權第三方應用後,微信會拉起應用或重定向到第三方網站,而且帶上受權臨時票據code參數;

  2.  經過code參數加上AppID和AppSecret等,經過API換取access_token;

  3.  經過access_token進行接口調用,獲取用戶基本數據資源或幫助用戶實現基本操做。

獲取access_token 時序圖

 

具體流程請參考官方文檔,咱們這裏只說一下python的實現方法。官方文檔地址 點這裏

 

參考python-instagram 我寫了一個 python-weixin (https://github.com/zongxiao/python-weixin)一個微信python SDK

不過如今還只有微信接入、獲取用戶信息、 刷新refresh_token 等簡單功能

首先 須要把代碼clone到本地

而後執行 

python setup.py install

使用方式很是簡單

 1 from weixin.client import WeixinAPI
 2 
 3 APP_ID = 'your app id'
 4 APP_SECRET = 'your app secret'
 5 REDIRECT_URI = 'http://your_domain.com/redirect_uri'  # 這裏必定要注意 地址必定要加上http/https
 6 
 7 scope = ("snsapi_login", )
 8 api = WeixinAPI(appid=APP_ID,
 9                         app_secret=APP_SECRET,
10                         redirect_uri=REDIRECT_URI)
11 
12 authorize_url = api.get_authorize_url(scope=scope)

如今將 

authorize_url 地址在瀏覽器打開, 將跳轉到微信登陸頁面,使用手機掃碼登陸後將跳轉到
http://your_domain.com/redirect_uri?code=CODE&state=STATE 頁面

如今咱們就可使用code 來獲取登陸的 access_token

access_token = api.exchange_code_for_access_token(code=code)

access_token 信息爲

{ 
"access_token":"ACCESS_TOKEN", 
"expires_in":7200, 
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID", 
"scope":"SCOPE" 
}
參數 說明
access_token 接口調用憑證(有效期目前爲2個小時)
expires_in access_token接口調用憑證超時時間,單位(秒)
refresh_token 用戶刷新access_token(有效期目前爲30天)
openid 受權用戶惟一標識
scope 用戶受權的做用域,使用逗號(,)分隔

 

獲取access_token後,就能夠進行接口調用,有如下前提:

  1.  access_token有效且未超時;

  2.  微信用戶已受權給第三方應用賬號相應接口做用域(scope)。

對於接口做用域(scope),能調用的接口有如下:

受權做用域(scope) 接口 接口說明
snsapi_base /sns/oauth2/access_token 經過code換取access_token、refresh_token和已受權scope
/sns/oauth2/refresh_token 刷新或續期access_token使用
/sns/auth 檢查access_token有效性
snsapi_userinfo /sns/userinfo 獲取用戶我的信息

 

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

# 刷新或續期access_token使用
refresh_token = api.exchange_refresh_token_for_access_token(refresh_token=auth_info['refresh_token'])

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

# 獲取用戶我的信息
user = api.user(openid=auth_info['openid'])

# 檢查access_token有效性
v = api.validate_token(openid=auth_info['openid'])

 

如今就微信登陸就完成了

下面是用 flask 實現的完整的例子

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 = 'appid'
APP_SECRET = 'app secret'
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)

 

參考連接:

微信網站應用接入文檔 

網站應用建立地址

python-weixin github 地址  https://github.com/zongxiao/python-weixin

相關文章
相關標籤/搜索