Today we are going to log in to the wechat open platform
First, link to the document: https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html
1. After the third party initiates the wechat authorization login request, and the wechat user allows to authorize the third party application, wechat will pull up the application or redirect to the third party website, and bring the code parameter of the authorization temporary bill; 2. Through the code parameter plus AppID and AppSecret, access_token is exchanged through API; 3. Call the interface through access_token to obtain the basic data resources of users or help users realize basic operations.
It's clear what the document says
First, we need to register an open platform account
But it's not good that the enterprise certification is required to spend 300 RMB for qualification certification.
ok, let's create a website application first
That's how it goes
After filling in, you can start the development of wechat login when the status is passed.
And then we need to know three things
ok, the configuration information is complete
Let's continue to see the documents of wechat open platform
Step 1: request code
The request here needs to be accompanied by the corresponding parameters
Redirect \ uri is the callback address uri. Just configured authorization callback field is filled in and the interface or html is added. This can be based on your own business. Here I am filling in the interface
This urlEnCode handles many online tools: https://tool.chinaz.com/tools/urlencode.aspx
Then fill in the corresponding information according to the wechat development document
https://open.weixin.qq.com/connect/qrconnect?
appid=xxxxxxxxx&redirect_uri=https%3a%2f%2fwww.baidu.com%2frequestWechatLogin&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect
I fill in the above information casually. Please fill in according to your own information during development
ok, open this link and you will enter the page, and you can start to log in.
Use wechat scanning on the mobile terminal,
Note: every time each user is authorized, they will get a one-time code. This code can only be used once, and the next time they are authorized to log in, the code will be generated again!!!
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
Link parameters are filled in according to wechat open platform configuration
Because what I fill in in the redirect ﹣ URI is an interface, I need to write an interface, and I will directly go to the code. There are encapsulated classes and enumerations here, and I will not show you that you can modify it!!!
1 /** 2 * Request code web wechat login 3 * 4 * @param code Request login unique code 5 * @return General return object 6 */ 7 @ApiOperation("request code WeChat login") 8 @ApiImplicitParam(name = "code", value = "Request login unique code", dataType = "String", paramType = "query") 9 @GetMapping("/requestWeChatLogin") 10 public ApiResult requestWeChatLogin(@RequestParam("code") String code) { 11 // Throw an exception if the assertion is empty 12 Assert.notNull(code, "Unauthorized success"); 13 String appId = "xxxxxxxxx"; 14 String secret = "xxxxxxxxxxxxxxxxxx"; 15 String strUri = StrUtil.format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={}&secret={}&code={}&grant_type=authorization_code", appId, secret, code); 16 // Use Hutool Development kit, implementation request interface, according to code Get relevant information 17 HttpResponse response = HttpRequest.get(strUri).execute(); 18 // Turn into JSON object 19 JSONObject jsonObject = JSONUtil.parseObj(response.body()); 20 // judge JSON In object unionid Does it exist? 21 if (jsonObject.isNull("unionid")) { 22 // No return results exist, code invalid 23 return new ApiResult(CommonEnum.CODE_INVALID); 24 } 25 // Obtain unionId 26 String unionId = jsonObject.get("unionid").toString(); 27 // Obtain openId 28 String openId = jsonObject.get("openid").toString(); 29 // Obtain accessToken 30 String accessToken= jsonObject.get("access_token").toString(); 31 return new ApiResult(CommonEnum.SUCCESS); 32 }
After such an operation, you can obtain some information of the authorized users according to the code,
For example, you can get the user's personal information according to access_token & openid
The save database can be saved, there are other operations to write by yourself!!!