Client-side development-step for logging on to the applet, and step for logging on to the Applet
The main purpose of this applet is to grant user information authorization to your database after logging on with user information, in this way, the openid obtained by the code obtained by each login can be found in the project database.
During the test, You need to log on to the code, so you will use the development tool ~!
Use of this tool must be authorized by the Project
The generated code has a time limit.
The logon port api is as follows:
Interface address: https://api.weixin.qq.com/sns/jscode2session? Appid = APPID & secret = SECRET & js_code = JSCODE & grant_type = authorization_code
Request Parameters:
Required parameter description
Appid is the unique identifier of a applet.
Secret is the app secret of the Applet
Js_code is the code obtained at logon.
Grant_type is set to authorization_code
Response parameters:
Parameter description
Unique openid
Session_key session key
Expires_in indicates the session validity period in seconds. For example, 2592000 indicates that the session is valid for 30 days.
Return description:
// Normally returned JSON data packet {"openid": "OPENID", "session_key": "SESSIONKEY" "expires_in": 2592000}
// Return a JSON packet when an error occurs (the Code in this example is invalid) {"errcode": 40029, "errmsg": "invalid code "}
So we can write the code as follows:
The company has written all the message formats sent by myself. I only need to write the Request Path and request parameters to OK.
// @ Param code the code generated by the user login // @ Return OAuthResult the public OAuthResult getOAuthResultByCode (String code) class containing openid and session_key and expires_in is returned) {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 exception, cause:" + oAuthResult. getErrmsg ();} return oAuthResult ;}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.