Java 后端 本地调试-获取微信公众号 openId
创始人
2024-02-09 01:07:48
0

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

  • 申请测试微信公众号
  • 内网穿透工具
  • 配置公众号
  • 获取用户 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";@Autowiredprivate 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 checkreturn 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);// 校验 tokenString 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

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...