Java 后端 本地调试-获取微信公众号 openId

4 篇文章 0 订阅

Java 后端 本地调试-获取微信公众号 openId

申请测试微信公众号

微信测试公众号

内网穿透工具

netapp

配置公众号

  1. 搜索网页账号选项
    在这里插入图片描述
  2. 点击修改,填写内网穿透的域名
    在这里插入图片描述

获取用户 openId

1 第一步:用户同意授权,获取code
2 第二步:通过 code 换取网页授权access_token
3 第三步:刷新access_token(如果需要)
4 第四步:拉取用户信息(需 scope 为 snsapi_userinfo)
5 附:检验授权凭证(access_token)是否有效
– 来源https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html

第2步 就可以获取到 openId 了
代码如下

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;
    }
    
}

debug启动服务之后,执行以下步骤

  1. 获取微信客户端url
    访问 http://shootercheng.nat300.top/wx/client
    在这里插入图片描述
    在这里插入图片描述
  2. 使用微信打开此地址
    在这里插入图片描述
    如图,已在本地调试获取到 openId
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chengdu.S

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值