search

How to get token value

Jan 27, 2021 pm 05:27 PM
token

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 calls wx.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 key session_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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),