欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Python怎么實(shí)現(xiàn)微信小程序登錄api

本篇內(nèi)容主要講解“Python怎么實(shí)現(xiàn)微信小程序登錄api”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Python怎么實(shí)現(xiàn)微信小程序登錄api”吧!

創(chuàng)新互聯(lián)建站專(zhuān)注于企業(yè)成都營(yíng)銷(xiāo)網(wǎng)站建設(shè)、網(wǎng)站重做改版、西峰網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開(kāi)發(fā)成都商城網(wǎng)站開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性?xún)r(jià)比高,為西峰等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。

一、先來(lái)看看效果

Python怎么實(shí)現(xiàn)微信小程序登錄api

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

Python怎么實(shí)現(xiàn)微信小程序登錄api

二、官方登錄流程圖

Python怎么實(shí)現(xiàn)微信小程序登錄api

三、小程序登錄流程梳理:

1、小程序端調(diào)用wx.login

2、判斷用戶(hù)是否授權(quán)

3、小程序端訪(fǎng)問(wèn) wx.getuserinfo

4、小程序端js代碼:

wx.login({
 success: resp => {
 // 發(fā)送 res.code 到后臺(tái)換取 openid, sessionkey, unionid
 console.log(resp);
 var that = this;
 // 獲取用戶(hù)信息
 wx.getsetting({
 success: res => {
 if (res.authsetting['scope.userinfo']) {
 // 已經(jīng)授權(quán),可以直接調(diào)用 getuserinfo 獲取頭像昵稱(chēng),不會(huì)彈框
 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) { },//請(qǐng)求失敗
			 complete: function () { }//請(qǐng)求完成后執(zhí)行的函數(shù)
			 })
 }
 })
 } 
 }
 })
 }
 })

5、后端服務(wù)器訪(fǎng)問(wèn)code2session,通過(guò)code2session這個(gè)api接口來(lái)獲取真正需要的微信用戶(hù)的登錄態(tài)session_key和 openid 和 unionid

6、后端服務(wù)器校驗(yàn)用戶(hù)信息,對(duì)encrypteddata 解密
微信小程序登錄后獲得session_key后,返回了encrypteddata,iv的數(shù)據(jù),其中encrypteddata解密后包含了用戶(hù)的信息,解密后的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這邊一般會(huì)遇到modulenotfounderror:no module named "crypto"錯(cuò)誤
(1)執(zhí)行pip3 install pycryptodome
(2)如果還是提示沒(méi)有該模塊,那就虛擬環(huán)境目錄lib—-site-package中查看是否有crypto文件夾,這時(shí)你應(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' # 開(kāi)發(fā)者關(guān)于微信小程序的appid
 appsecret = 'appsecret' # 開(kāi)發(fā)者關(guān)于微信小程序的appsecret
 code = data['platcode'] # 前端post過(guò)來(lái)的微信臨時(shí)登錄憑證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請(qǐng)求
 resdata = response_data.json()
 openid = resdata ['openid'] # 得到用戶(hù)關(guān)于當(dāng)前小程序的openid
 session_key = resdata ['session_key'] # 得到用戶(hù)關(guān)于當(dāng)前小程序的會(huì)話(huà)密鑰session_key

 pc = wxbizdatacrypt(appid, session_key) #對(duì)用戶(hù)信息進(jìn)行解密
 userinfo = pc.decrypt(encrypteddata, iv) #獲得用戶(hù)信息
 print(userinfo)
 '''
 下面部分是通過(guò)判斷數(shù)據(jù)庫(kù)中用戶(hù)是否存在來(lái)確定添加或返回自定義登錄態(tài)(若用戶(hù)不存在則添加;若用戶(hù)存在,返回用戶(hù)信息)
 
 --------略略略略略略略略略-------------
 
 這部分我就省略啦,數(shù)據(jù)庫(kù)中對(duì)用戶(hù)進(jìn)行操作
 '''
 
 return json.dumps
({
"code": 200, "msg": "登錄成功","userinfo":userinfo}, indent=4, sort_keys=true, default=str, ensure_ascii=false)

到此,相信大家對(duì)“Python怎么實(shí)現(xiàn)微信小程序登錄api”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!

文章名稱(chēng):Python怎么實(shí)現(xiàn)微信小程序登錄api
當(dāng)前網(wǎng)址:http://www.chinadenli.net/article2/jiigoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)小程序開(kāi)發(fā)移動(dòng)網(wǎng)站建設(shè)做網(wǎng)站手機(jī)網(wǎng)站建設(shè)面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司