WeChat client development-step for logging on to the applet,-Applet

Source: Internet
Author: User
Tags openid

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.