This article mainly introduces the development of WeChat--the steps to log in to the mini program. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
The main purpose of this small program is to allow users to log in with WeChat user information and authorize the user information to be stored in their own database, so that every time they log in to WeChat, they will get the code The obtained openid can be used to find the relevant information of the user in the project database.
During the testing process, the code obtained by the user login is required, so WeChat development gadgets will be used~!
The use of this tool must be authorized by the project
The generated code is time limited
The api of the WeChat login port is as follows
Interface address: http://ipnx.cn/
Request parameters:
Parameters Required instructions
appid is the unique identifier of the mini program
secret is the app secret of the mini program
js_code is the code obtained when logging in
grant_type is filled in as authorization_code
Return parameters:
Parameter Description
openid User unique identification
session_key Session key
expires_in Session validity period, in seconds, for example, 2592000 means that the session validity period is 30 days
Return instructions:
//正常返回的JSON數(shù)據(jù)包 { "openid": "OPENID", "session_key": "SESSIONKEY" "expires_in": 2592000 }
//錯誤時返回JSON數(shù)據(jù)包(示例為Code無效) { "errcode": 40029, "errmsg": "invalid code" }
So we can write the code as follows
The company will send the message format itself It’s all written, I just need to write the request path and request parameters.
//@Param code 用戶登錄微信生成的code //@Return OAuthResult 返回包含openid和session_key和expires_in的類 public OAuthResult getOAuthResultByCode(String code) { String path = "/sns/jscode2session"; Map<String, Object> parameters = new HashMap<>(); parameters.put("appid", WechatConstant.WECHAT_APP_ID); parameters.put("secret", WechatConstant.WECHAT_APP_SECRET); parameters.put("js_code", code); parameters.put("grant_type", "authorization_code"); OAuthResult oAuthResult = messageSender.getMessageForObject(path, parameters, OAuthResult.class, false); if (oAuthResult.getOpenid() == null){ throw new BusinessException(BusinessException.Code.OAUTH_PROCESS_ERROR, "OAuth異常,原因:" + oAuthResult.getErrmsg()); } return oAuthResult; }
The above is the entire content of this article. I hope it will be helpful to everyone’s study. I also hope that everyone will support Script House.
The above is the detailed content of How to log in to the mini program for WeChat development. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)