1. 程式人生 > 程式設計 >python實現網站微信登入的示例程式碼

python實現網站微信登入的示例程式碼

最近微信登入開放公測,為了方便微信使用者使用,我們的產品也決定加上微信登入功能,然後就有了這篇筆記。

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

python實現網站微信登入的示例程式碼

微信現在提供兩種登入接入方式

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

這裡我們使用的是網站應用微信登入

按照 官方流程

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

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

2 建立網站應用

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

3 接入微信登入

在資源中心查閱網站應用開發文件,開發接入微信登陸功能,讓使用者可使用微信登入你的網站應用

如果已經完成上面的操作,請繼續往下看

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

微信OAuth2.0授權登入目前支援authorization_code模式,適用於擁有server端的應用授權。該模式整體流程為:

  1. 第三方發起微信授權登入請求,微信使用者允許授權第三方應用後,微信會拉起應用或重定向到第三方網站,並且帶上授權臨時票據code引數;
  2. 通過code引數加上AppID和AppSecret等,通過API換取access_token;
  3. 通過access_token進行介面呼叫,獲取使用者基本資料資源或幫助使用者實現基本操作。

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

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

不過現在還只有微信接入、獲取使用者資訊、 重新整理refresh_token 等簡單功能

首先 需要把程式碼clone到本地

然後執行

python setup.py install

使用方式非常簡單

from weixin.client import WeixinAPI

APP_ID = 'your app id'
APP_SECRET = 'your app secret'
REDIRECT_URI = 'http://your_domain.com/redirect_uri' # 這裡一定要注意 地址一定要加上http/https

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

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,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,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,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" rel="external nofollow" >weixin login!</a>') % '/login'

if __name__ == "__main__":
 app.run(debug=True)

參考連結:

微信網站應用接入文件

網站應用建立地址

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。