Micro-letters get paid openid

I. Preparation Tool

No matter what development, official documents should be the first thought of the official document posted here: micro letter page document authorizing
addition, we also need tools that allow a network to penetrate the micro-channel can be accessed in our development environment domain name. I'm using natapp. There are many such tools online, you may find yourself.
Here we use the micro-channel provided test account as demo

 

Second, begin to develop

Network penetration is not demonstrated here, let straight into the subject:
1. Fill pages authoritative name
at the beginning of this document on a buried pit

 

 This passage means that we need in the picture frame red fill the position of domain name we want to develop in front of the development. Here, we have a test environment, so we need to fill in your own domain at this location test account management page, fill in here should pay attention to the rules of the domain name.

 

 

 2. Read the document
and then read the document we can see that there are two pages authorized scope,
snsapi_base and snsapi_userinfo. Two kinds of scope are acquired opeid, except that in addition to snsapi_userinfo openid may also obtain the basic information of the user, but the user needs to manually confirm.
Read on and then we can see the official document authorization step

 

  • The first step: user consent, access code
  • Step two: by code pages in exchange for access_token authorization and openid
  • The third step: to refresh access_token (if needed)
  • The fourth step: pulling the user information (as required scope snsapi_userinfo)

We just need openid, so we just developed to the second step just fine. Here we are pressing official steps to develop.

3. Get the code
after viewing the document we found that we needed stitching and a url to access it. Parameters url of the document written very clearly. Look at his example could look at it. Here is more important redirect_uri. This parameter is filled in a link. We will automatically forward after url to access the link and the value of the state of the code and we need to splice url as a parameter. This value redirect_uri to be filled in our code of position controller.
So here we need a piece of code:

 

@RestController
@RequestMapping("/weixin")
@ slf4j
public class WeixinController {

    @GetMapping("/auth")
    public void auth(@RequestParam("code") String code,@RequestParam("state") String state){
        log.info ( "auth began ...." );
        log.info("code={}",code);
        log.info("state={}",state);
    }
}

I spliced ​​url, here we should note that stitching according to their own situation. The need for access to micro letter app after successfully splicing

https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxx&redirect_uri=http://xxx/sell/weixin/auth&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect

The result of access

 

 So we got the code
4. get openid
continue to view the document and found that we only need to use the code to get access to another url and then you can get to what we wanted. Following the above code

@RestController
@RequestMapping("/weixin")
@ slf4j
public class WeixinController {

    @GetMapping("/auth")
    public void auth(@RequestParam("code") String code,@RequestParam("state") String state){
        log.info ( "auth began ...." );
        log.info("code={}",code);
        log.info("state={}",state);
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=xxxx&secret=xxxx&code="+code+"&grant_type=authorization_code";
        Template Residual rest template = new Rest Template ();
        String result = restTemplate.getForObject(url, String.class);
        log.info("result={}",result);
    }
}

Then we visit again the third step of splicing url you can see the following results.

 

 Format the results we can see what we want openid

 

 

Third, the summary

Process looks very simple. But more than just one of the simplest, most direct manual get openid example. Real need in the course of its own business processes to be developed, then it may be some trouble. At this point we can also use some third-party online sdk to develop. For example: weixin-java-tools.

This summary is rather vague, there is no method to get openid summed up as direct, but if you forget it, this allows you to quickly pick it up.

Guess you like

Origin www.cnblogs.com/qingmuchuanqi48/p/11627940.html