本篇內(nèi)容主要講解“Python怎么實現(xiàn)微信小程序登錄api”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Python怎么實現(xiàn)微信小程序登錄api”吧!
創(chuàng)新互聯(lián)建站專注于企業(yè)成都營銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、西峰網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開發(fā)、成都商城網(wǎng)站開發(fā)、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為西峰等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
一、先來看看效果

接口請求返回的數(shù)據(jù):

二、官方登錄流程圖

三、小程序登錄流程梳理:
1、小程序端調(diào)用wx.login
2、判斷用戶是否授權(quán)
3、小程序端訪問 wx.getuserinfo
4、小程序端js代碼:
wx.login({
success: resp => {
// 發(fā)送 res.code 到后臺換取 openid, sessionkey, unionid
console.log(resp);
var that = this;
// 獲取用戶信息
wx.getsetting({
success: res => {
if (res.authsetting['scope.userinfo']) {
// 已經(jīng)授權(quán),可以直接調(diào)用 getuserinfo 獲取頭像昵稱,不會彈框
wx.getuserinfo({
success: userresult => {
var platuserinfomap = {}
platuserinfomap["encrypteddata"] = userresult.encrypteddata;
platuserinfomap["iv"] = userresult.iv;
wx.request({
url: 'http://127.0.0.1:5000/user/wxlogin',
data: {
platcode: resp.code,
platuserinfomap: platuserinfomap,
},
header: {
"content-type": "application/json"
},
method: 'post',
datatype:'json',
success: function (res) {
console.log(res)
wx.setstoragesync("userinfo", res.userinfo) //設(shè)置本地緩存
},
fail: function (err) { },//請求失敗
complete: function () { }//請求完成后執(zhí)行的函數(shù)
})
}
})
}
}
})
}
})5、后端服務(wù)器訪問code2session,通過code2session這個api接口來獲取真正需要的微信用戶的登錄態(tài)session_key和 openid 和 unionid
6、后端服務(wù)器校驗用戶信息,對encrypteddata 解密
微信小程序登錄后獲得session_key后,返回了encrypteddata,iv的數(shù)據(jù),其中encrypteddata解密后包含了用戶的信息,解密后的json格式如下:
{
"openid": "openid",
"nickname": "nickname",
"gender": gender,
"city": "city",
"province": "province",
"country": "country",
"avatarurl": "avatarurl",
"unionid": "unionid",
"watermark":
{
"appid":"appid",
"timestamp":timestamp
}
}7、新建解密文件——wxbizdatacrypt.py
from crypto.cipher import aes這邊一般會遇到modulenotfounderror:no module named "crypto"錯誤
(1)執(zhí)行pip3 install pycryptodome
(2)如果還是提示沒有該模塊,那就虛擬環(huán)境目錄lib—-site-package中查看是否有crypto文件夾,這時你應(yīng)該看到有crypto文件夾,將其重命名為crypto即可
import base64
import json
from crypto.cipher import aes
class wxbizdatacrypt:
def __init__(self, appid, sessionkey):
self.appid = appid
self.sessionkey = sessionkey
def decrypt(self, encrypteddata, iv):
# base64 decode
sessionkey = base64.b64decode(self.sessionkey)
encrypteddata = base64.b64decode(encrypteddata)
iv = base64.b64decode(iv)
cipher = aes.new(sessionkey, aes.mode_cbc, iv)
decrypted = json.loads(self._unpad(cipher.decrypt(encrypteddata)))
if decrypted['watermark']['appid'] != self.appid:
raise exception('invalid buffer')
return decrypted
def _unpad(self, s):
return s[:-ord(s[len(s)-1:])]8、flask的/user/wxloginapi代碼:
import json,requests
from wxbizdatacrypt import wxbizdatacrypt
from flask import flask
@app.route('/user/wxlogin', methods=['get','post'])
def user_wxlogin():
data = json.loads(request.get_data().decode('utf-8')) # 將前端json數(shù)據(jù)轉(zhuǎn)為字典
appid = 'appid' # 開發(fā)者關(guān)于微信小程序的appid
appsecret = 'appsecret' # 開發(fā)者關(guān)于微信小程序的appsecret
code = data['platcode'] # 前端post過來的微信臨時登錄憑證code
encrypteddata = data['platuserinfomap']['encrypteddata']
iv = data['platuserinfomap']['iv']
req_params = {
'appid': appid,
'secret': appsecret,
'js_code': code,
'grant_type': 'authorization_code'
}
wx_login_api = 'https://api.weixin.qq.com/sns/jscode2session'
response_data = requests.get(wx_login_api, params=req_params) # 向api發(fā)起get請求
resdata = response_data.json()
openid = resdata ['openid'] # 得到用戶關(guān)于當前小程序的openid
session_key = resdata ['session_key'] # 得到用戶關(guān)于當前小程序的會話密鑰session_key
pc = wxbizdatacrypt(appid, session_key) #對用戶信息進行解密
userinfo = pc.decrypt(encrypteddata, iv) #獲得用戶信息
print(userinfo)
'''
下面部分是通過判斷數(shù)據(jù)庫中用戶是否存在來確定添加或返回自定義登錄態(tài)(若用戶不存在則添加;若用戶存在,返回用戶信息)
--------略略略略略略略略略-------------
這部分我就省略啦,數(shù)據(jù)庫中對用戶進行操作
'''
return json.dumps
({
"code": 200, "msg": "登錄成功","userinfo":userinfo}, indent=4, sort_keys=true, default=str, ensure_ascii=false)到此,相信大家對“Python怎么實現(xiàn)微信小程序登錄api”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
文章名稱:Python怎么實現(xiàn)微信小程序登錄api
當前網(wǎng)址:http://chinadenli.net/article2/jiigoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、小程序開發(fā)、移動網(wǎng)站建設(shè)、做網(wǎng)站、手機網(wǎng)站建設(shè)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)