Home>Article>WeChat Applet> How to get token value

How to get token value

coldplay.xixi
coldplay.xixi Original
2021-01-27 17:27:13 52970browse

Method to obtain the token value: 1. The applet calls [wx.login()] to obtain the temporary login credential code and returns it to the developer server; 2. The developer server exchanges the code for the user's unique identification openid and session key [session_key].

How to get token value

The operating environment of this article: Windows 7 system, WeChat version 3.1.2, Dell G3 computer.

Method to obtain the token value:

1. The applet callswx.login()to obtain the temporary login credential code and return it to Developer server.

2. The developer server exchanges code for the user's unique identifier openid and session keysession_key.

The developer server can then generate a custom login state based on the user ID, which can be used to identify the user's identity during subsequent front-end and back-end interactions in subsequent business logic.

// 登录 wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId // console.log(res) if (res.code) { //发起网络请求 wx.request({ url: 'url', method: 'POST', data: { // x: '', // y: '' code: res.code //将code发给后台拿token }, header: { 'content-type': 'application/json' // 默认值 }, success: function(res) { // 存token console.log('token=' + res.data.data.token) that.globalData.token = res.data.data.token; //拿到后将token存入全局变量 以便其他页面使用 } }) } else { console.log('获取用户登录态失败!' + res.errMsg) } } })

Login Credential Verification

The temporary login credential verification interface is an HTTPS interface. The developer server uses the temporary login credential code to obtain session_key and openid, etc.

In fact, what we have to do is to send the code value to the backend, and the backend can return the token value to us after requesting the corresponding interface! In other pages, token is also needed to request data, so when we get it, we need to save it in a global variable so that the page can get it directly (getApp() .globalData.token).

Related free learning recommendations:

WeChat Mini Program Development

The above is the detailed content of How to get token value. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is WeChat h5 page Next article:What is WeChat h5 page