日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

最近我們的網站要加微信登錄功能,找了Python sdk 感覺都不滿意,然后就參考instagram python sdk 自己造了輪子。

輪子 github 地址 python-weixin

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

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

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

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

按照 官方流程

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

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

  1. 創建網站應用

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

  1. 接入微信登錄

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

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

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

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

  1. 第三方發起微信授權登錄請求,微信用戶允許授權第三方應用后,微信會拉起應用或重定向到第三方網站,并且帶上授權臨時票據code參數;
  2. 通過code參數加上AppID和AppSecret等,通過API換取access_token;
  3. 通過access_token進行接口調用,獲取用戶基本數據資源或幫助用戶實現基本操作。

獲取access_token 時序圖

網站微信登錄-python 實現

 

獲取access_token 時序圖

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

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

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


安裝

方法一 手動安裝

  1. 首先 需要把代碼clone到本地
  2. python setup.py install

方法二 pip install

pip install git+https://github.com/gusibi/python-weixin.git@master

使用方式

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://yoursite.com/login/weixin)在瀏覽器打開, 將跳轉到微信登錄頁面,使用手機掃碼登錄后將跳轉到

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和已授權scopesnsapi_base/sns/oauth2/refresh_token刷新或續期access_token使用snsapi_base/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)

參考鏈接:

  • 微信網站應用接入文檔(https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN)
  • 網站應用創建地址(https://open.weixin.qq.com/cgi-bin/frame?t=home/web_tmpl&lang=zh_CN)
  • python-weixin(https://github.com/gusibi/python-weixin)

分享到:
標簽:登錄
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定