国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

網站微信登錄-python實現

2019-11-14 17:22:26
字體:
來源:轉載
供稿:網友

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

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

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

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

這里我們使用的是網站應用微信登錄

按照 官方流程

1 注冊并通過開放平臺開發者資質認證

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

2 創建網站應用

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

3 接入微信登錄

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

 

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

 

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

微信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_inaccess_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 Flaskfrom flask import Markupfrom flask import redirectfrom flask import requestfrom flask import jsonifyfrom weixin.client import WeixinAPIfrom weixin.oauth2 import OAuth2AuthExchangeErrorapp = 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

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙泉市| 霸州市| 田阳县| 元氏县| 澄迈县| 衡阳县| 布尔津县| 黄大仙区| 林甸县| 农安县| 石台县| 汝南县| 仲巴县| 留坝县| 牟定县| 韶山市| 彩票| 罗田县| 海城市| 桦川县| 宜川县| 鱼台县| 沁水县| 东乡县| 沁阳市| 九龙坡区| 南通市| 张家港市| 云阳县| 锡林郭勒盟| 满城县| 贵阳市| 定日县| 武穴市| 西乌珠穆沁旗| 崇礼县| 饶阳县| 神池县| 广河县| 梨树县| 浑源县|