当前位置:网站首页>【项目实训】微信公众号获取用户openid
【项目实训】微信公众号获取用户openid
2022-06-12 01:13:00 【par_ser】
首先,为了方便公众号开发测试,可以申请测试号,地址如下
向指定用户推送消息,通常需要他的openId,要获取用户的openId,可以通过微信公众号的网页授权回调(搭配一个用户绑定页面,可以将微信公众号用户与我们自己的系统的用户建立联系)

上面是微信开发文档的说明,可以看到如果我们仅仅想做用户绑定,然后推送模板消息的话,那么scope只需要为snsapi_base
@GetMapping("/getUserCode")
public String getUserCode(){
String backUrl = "http://ip端口/wx/getUserOpenId";
String getOpenIdUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri="+ backUrl+"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
getOpenIdUrl = getOpenIdUrl.replace("APPID",appid);
return "redirect:" + getOpenIdUrl;
}
/**
* 获取用户openId
* @return
* @throws IOException
*/
@GetMapping("/getUserOpenId")
public String getUserOpenId(HttpServletRequest request)throws IOException{
//获取code
String code = request.getParameter("code");
System.out.println("code:"+code);
if(code== null){
System.out.println("code为空");
}
String openid = WechatUserUtil.exchangeCode2OpenId(code,appid,secret);
return openid;
}
public static String exchangeCode2OpenId(String code,String appId,String appSecret) {
String openid = "";
try {
// 换取access_token 其中包含了openid
// 这里通过code换取的是一个特殊的网页授权access_token,与基础支持中的access_token(该access_token用于调用其他接口)不同。
String URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"
.replace("APPID", appId).replace("SECRET", appSecret).replace("CODE", code);
String jsonStr = HttpUtil.sendGet(URL);
System.out.println("----------微信换取openid返回的结果:{}----------");
Gson gson = new Gson();
WechatOpenId wechatOpenId = gson.fromJson(jsonStr, WechatOpenId.class);
if (null != wechatOpenId) {
openid = wechatOpenId.getOpenid();
}
} catch (Exception e) {
System.out.println("----------微信换取openid发生了异常:{}----------");
}
return openid;
}边栏推荐
- [answer] in the business sequence diagram of XX shopping mall, is it drawn as a business executor (bank) or a business entity (banking system)
- Investment analysis and prospect forecast report of wearable biosensor industry for global and Chinese medical monitoring 2022 ~ 2028
- Article 3: Design of multifunctional intelligent trunk following control system | undergraduate graduation project - [defense ppt]
- Lambda create stream
- C language bit operation - learning 26
- Comparison of OpenCV basic codes of ros2 foxy~galactic~humble
- Learn to crawl steadily 07 - detailed explanation of how to use XPath
- Ms-hgat: information diffusion prediction based on memory enhanced sequence hypergraph attention network
- Virtual human appears on the stage of the Winter Olympic Games, connecting elements of the meta universe
- One article to show you how to understand the harmonyos application on the shelves
猜你喜欢

Matlab 基础应用02 wind 股票数据介绍和使用案例:

Explain asynchronous tasks in detail: the task of function calculation triggers de duplication

Kill, pkill, killall, next, what I brought to you in the last issue is how to end the process number and rush!

How to guarantee industrial control safety: system reinforcement

How can functional tests be quickly advanced in one month? It is not a problem to clarify these two steps

Zhongchuang patents | China has 18000 necessary patents for 5g standards, respects intellectual property rights and jointly builds a strong intellectual property country

Ms-hgat: information diffusion prediction based on memory enhanced sequence hypergraph attention network

中创专利|中国5G标准必要专利达1.8万项,尊重知识产权,共建知识产权强国

Make good use of these 28 tools, and the development efficiency soars

MS-HGAT: 基于记忆增强序列超图注意力网络的信息扩散预测
随机推荐
Lambda中间操作skip
Article 3: Design of multifunctional intelligent trunk following control system | undergraduate graduation project - [defense ppt]
System. Commandline option
Lambda中间操作limit
Current situation investigation and demand forecast report of global and Chinese phenolic resin market, 2022 Edition
C language pointer - learning 22
Some suggestions on writing code to reproduce the paper!
Elegant throttling / de buffeting decorator under LAYA
Argodb 3.2 of star ring technology was officially released to comprehensively upgrade ease of use, performance and security
Inventory: more than 20 typical safety incidents occurred in February, with a loss of nearly $400million
[answer] should the role with one end of the reflexive association be called "current version"
flowable 工作流
给你一个项目,你将如何开展性能测试工作?
Vscode - the problem of saving a file and automatically formatting the single quotation mark 'into a double quotation mark'
[signalr complete series] Implementation of signalr packet communication in net6
Learn to crawl steadily 07 - detailed explanation of how to use XPath
王希廷博士:从知识图谱和自然语言生成的角度认识可解释推荐
2022 edition of global and Chinese on silicon liquid crystal market supply and demand research and prospect Trend Forecast Report
河南中创|从云到边,边缘计算如何赋能数据中心
Make good use of these 28 tools, and the development efficiency soars