微信小程序前端获取OpenID和session_key
code2Session:
json
https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
js
wx.login({
success: (res) => {
console.log(res.code) //拿到code
uni.request({
url:
`https://api.weixin.qq.com/sns/jscode2session?
appid=${appid}&secret=${密钥}&js_code=${res.code}&grant_type=authorization_code `,
method: 'GET',
success: (res) => {
const {openid,session_key} = res.data;//拿到openid和session_key
// 若要进行登录状态校验,需要在后端调用checkSessionKey接口
}
})
}
})
checkSessionKey:
http
https://api.weixin.qq.com/wxa/checksession?access_token=ACCESS_TOKEN&signature=SIGNATURE&openid=OPENID&sig_method=SIG_METHOD