Java backend local debugging - get WeChat official account openId

Java backend local debugging - get WeChat official account openId

Apply for a test WeChat public account

WeChat test public account

Intranet Penetration Tool

netapp

Configure official account

  1. Search web account options
    insert image description here
  2. Click Modify and fill in the domain name for intranet penetration
    insert image description here

Get user openId

1 Step 1: The user agrees to authorize and obtain the code
2 Step 2: Exchange the code for a webpage authorization access_token
3 Step 3: Refresh the access_token (if necessary)
4 Step 4: Pull user information (requires scope as snsapi_userinfo)
5 Attachments : Check whether the authorization certificate (access_token) is valid
– source https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html

In step 2, you can get the openId.
The code is as follows

package com.scd.serverless.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
 * @author James
 */
@RestController
@RequestMapping(value = "/wx")
public class WxController {
    
    
    private static final Logger LOGGER = LoggerFactory.getLogger(WxController.class);

    public static final String MAP_URL = "http://shootercheng.nat300.top";

    public static final String WE_CHAT_APP_ID = "wxfcfbe0c738b569a5";

    public static final String WE_CHAT_APP_SECRET = "002b379cbe62689eab9d0a7cfc227dd8";

    public static final String WE_CHAT_CLIENT_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";

    public static final String WE_CHAT_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";

    @Autowired
    private RestTemplate restTemplate;


    @RequestMapping(value = "/config")
    public String testConfig(HttpServletRequest request) {
    
    
        String signature = request.getParameter("signature");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");
        // 随机字符串
        String echostr = request.getParameter("echostr");
        LOGGER.info("config return info {}", echostr);
        // TODO check
        return echostr;
    }

    @RequestMapping(value = "/client")
    public String genWxUrl() throws UnsupportedEncodingException {
    
    
        String redirectUrl = MAP_URL + "/wx/callback?token=" + UUID.randomUUID();
        String encodeUrl = URLEncoder.encode(redirectUrl, StandardCharsets.UTF_8.name());
        String clientUrl = String.format(WE_CHAT_CLIENT_URL, WE_CHAT_APP_ID, encodeUrl);
        LOGGER.info("client url {}", clientUrl);
        return clientUrl;
    }

    @RequestMapping(value = "/callback")
    public String wxCallBack(HttpServletRequest request) throws JsonProcessingException {
    
    
        String code = request.getParameter("code");
        String token = request.getParameter("token");
        LOGGER.info("current token {}", token);
        // 校验 token
        String tokenUrl = String.format(WE_CHAT_ACCESS_TOKEN_URL, WE_CHAT_APP_ID, WE_CHAT_APP_SECRET, code);
        String result = restTemplate.getForObject(tokenUrl, String.class);
        LOGGER.info("token result {}", result);
        return result;
    }
    
}

After debug starts the service, perform the following steps

  1. Get WeChat client url
    access http://shootercheng.nat300.top/wx/client
    insert image description here
    insert image description here
  2. Use WeChat to open this address
    insert image description here
    As shown in the figure, the openId has been obtained through local debugging

Guess you like

Origin blog.csdn.net/modelmd/article/details/127992348