Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to achieve login credentials for WeChat Mini Programs API

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces WeChat Mini Programs API how to achieve login credentials related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that after reading this WeChat Mini Programs API how to achieve login credentials article will have a harvest, let's take a look at it.

Wx.login (OBJECT)

Call the API to obtain login credentials (code) in exchange for user login status information, including the user's unique identity (openid) and the login session key (session_key). The encryption and decryption communication of user data depends on the session key.

OBJECT parameter description:

The parameter name type is required to indicate whether successFunction API calls successful callback function failFunction No API call failed callback function completeFunction No API call ends callback function (both successful and failed calls will be executed)

Success return parameter description:

The parameter name type indicates the result of errMsgString call. After the codeString user is allowed to log in, the callback content will be code (valid for five minutes). The developer needs to send code to the developer server backend, exchange code for session_key api, and replace code with openid and session_key.

Sample code:

/ / app.jsApp ({onLaunch: function () {wx.login ({success: function (res) {if (res.code) {/ / initiate a network request wx.request ({url: 'https://test.com/onLogin',) Data: {code: res.code}})} else {console.log ('failed to get user login status!' + res.errMsg)});}}) code for session_key

This is a HTTPS interface, and the developer server uses login credentials code to get session_key and openid. Where session_key is the key to encrypt and sign user data. Session_key should not be transmitted over the network for the sake of its own application security.

Interface address:

Https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code

Request parameters:

The required parameter indicates that appid is the unique identification of Mini Program. Secret is the app secretjs_code of Mini Program. The codegrant_type obtained when logging in is authorization_code.

Return parameters:

Parameter describes the unique identifier of the openid user that uniquely identifies the session_key session key unionid user on the open platform. This field is returned only if certain conditions are met. For more information, please see the UnionID mechanism description.

Return description:

/ / normally returned JSON packets {"openid": "OPENID", "session_key": "SESSIONKEY"unionid": "UNIONID"} / / return JSON packets (invalid Code for example) {"errcode": 40029, "errmsg": "invalid code"} wx.checkSession (OBJECT)

The user login status obtained through the above interface has a certain timeliness. The longer users do not use Mini Program, the more likely they are to invalidate their login status. On the other hand, if the user has been using Mini Program, the user's login status remains valid. The specific prescription logic is maintained by Wechat and is transparent to developers. Developers only need to call the API wx.checkSession to check whether the current user login status is valid. After the login status expires, the developer can call wx.login to obtain the new user login status.

OBJECT parameter description:

The parameter name type is required to indicate whether the successFunction API calls a successful callback function, the login state does not expire the failed callback function, and the login state expires the callback function at the end of the API call (both successful and failed calls will be executed)

Sample code:

Wx.checkSession ({success: function () {/ / session is not expired and has been valid for the current lifecycle}, fail: function () {/ / login status expires wx.login () / / log in again. }) login status maintenance

After the user login status is obtained through wx.login (), the login state needs to be maintained. Developers should note that they should not directly use session_key, openid and other fields as the user's identity or session logo, but should send a session login status themselves (please refer to the login sequence diagram). Session generated by developers should be secure and should not be set for a long expiration time. After session is dispatched to the Mini Program client, it can be stored in storage for subsequent communication.

Check whether the user's login status is invalid by wx.checkSession (). And decide whether to call wx.login () to retrieve the login state.

Login sequence diagram Bug & Tip

Bug: iOS/Android 6.3.30. An exception occurs when calling wx.login in App.onLaunch

I believe that everyone has a certain understanding of the knowledge of "WeChat Mini Programs API how to achieve login credentials". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report