Java Web obtains WeChat authorized user information

Java Web obtains WeChat authorized user information

1. Apply for WeChat public test account

First, you need to go to the WeChat development public platform to apply for a WeChat public platform test number , the page is as follows.
Insert picture description here
After logging in, the page is as follows.
Insert picture description here
You will see this when you pull down the page, just scan the code on WeChat and follow.
Insert picture description here

No configuration is required for interface information. Scroll down the page to see the following page.
Insert picture description here
Click the modification behind the web account to fill in the url address. Note that the url address cannot carry http:// and https://. The correct format is as follows.
Insert picture description here
If there is no URL address, it can be obtained by the following method.

2. Get the URL address

. To obtain the URL address, first open the intranet penetration registration website , the registration step is omitted. To view the usage document after registration , we need to purchase a free web tunnel.
Insert picture description here
Then click Configure. Insert picture description here
Only the local port number needs to be configured, and the rest does not need to be configured. For example, the back-end port number is 8000. Fill in 8000 here. Choose different clients to download according to different systems.
Insert picture description here
After downloading, please refer to theInsert picture description here
operating mode as shown in the figure below . Here we take the windows system as an example. The interface after running is like this. At this point we need to enter the command line,Insert picture description here

cmd -authtoken=

Then enter the following command line

natapp -authtoken=9ab6b9040a624f40

The authtoken value here needs to be obtained here
Insert picture description here
. The following figure will appear after the correct command is entered. The yellow grid is the URL address we need.

Insert picture description here
Get the URL address, then we can configure it in the development of the WeChat official account platform.

3. Background code

1. After configuring at this time, we need to write Java code. First, we need a configuration entity class.

public class Oautch {
    
    
    public final static  String appID="wx7e96fc051d505d38";//微信appID
     public final static  String appSecret ="b6709177544ee8bd1cefc5466f9e6a66";//微信appSecret

    public final static  String REDIRECT_URI ="http://g6iq85.natappfree.cc"; //url地址
}

2. HttpClientUtil tool class
has been written here, please check HttpClientUti class for specific use .
3. Controller layer code.

  @ApiOperation("验证")
    @RequestMapping("/oautch")
    public void token(HttpServletResponse httpServletResponse) throws Exception {
    
    
                String path= Oautch.REDIRECT_URI+"/api/common/invoke";//这里拼接你的URL地址,用户授权同意之后会调用的接口方法。
                path= URLEncoder.encode(path,"UTF-8");
                String url="https://open.weixin.qq.com/connect/oauth2/authorize?" +
                        "appid="+Oautch.appID+
                        "&redirect_uri="+path+
                        "&response_type=code&" +
                        "scope=snsapi_userinfo&" +
                        "state=wechat_redirect";
                httpServletResponse.sendRedirect(url);
    }

Parameter introduction
Insert picture description here
When the user agrees to the authorization, we will get a code value, where state is the parameter value we passed in. When the user agrees, the value of state will also be passed in. We can judge through this state that the return message is Not returned by WeChat.
After the user agrees to the authorization, the following code will be executed.

 @ApiOperation("用户同意授权")
    @RequestMapping(value = "/invoke",produces="application/json; charset=utf-8")
    public void token1(String code, String state) throws IOException {
    
    
 /*
        * 得到code值,获取用户的openid获取token
        * */
      String path="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+Oautch.appID+"&secret="+Oautch.appSecret+"&code="+code+"&grant_type=authorization_code";
        JSONObject jsonObject = new JSONObject(HttpClientUtil.doGet(path));
       String access_token = (String) jsonObject.get("access_token");//得到token
       String openid = (String) jsonObject.get("openid");//得到openid
       //根据code值和oppenid获取用户的基本信息
               String path2="https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid+"&lang=zh_CN";
                     System.out.println(HttpClientUtil.doGet(path2));
}

Path parameter introduction
Insert picture description here
path2 parameter introduction
Insert picture description here
Return value parameter introduction, the return value here is a string, if necessary, it can be converted to json through the JSONObject class;
Insert picture description here
this is the end of the entire WeChat authorization to obtain user information.

Guess you like

Origin blog.csdn.net/zhangzhenkeai/article/details/109295496