当前位置:网站首页>[project training] wechat official account to obtain user openid
[project training] wechat official account to obtain user openid
2022-06-12 01:15:00 【par_ ser】
First , To facilitate the development and testing of official account , You can apply for a test number , The address is as follows
Push a message to a specified user , Usually need him openId, To get the user's openId, You can authorize callback through the web page of wechat official account ( With a user binding page , Wechat official account users can be connected with users of our own system )

The above is the description of wechat development documents , As you can see, if we just want to do user binding , Then push the template message , that scope Just for snsapi_base
@GetMapping("/getUserCode")
public String getUserCode(){
String backUrl = "http://ip port /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;
}
/**
* Get users openId
* @return
* @throws IOException
*/
@GetMapping("/getUserOpenId")
public String getUserOpenId(HttpServletRequest request)throws IOException{
// obtain code
String code = request.getParameter("code");
System.out.println("code:"+code);
if(code== null){
System.out.println("code It's empty ");
}
String openid = WechatUserUtil.exchangeCode2OpenId(code,appid,secret);
return openid;
}
public static String exchangeCode2OpenId(String code,String appId,String appSecret) {
String openid = "";
try {
// Exchange for access_token Which includes openid
// Through here code In exchange for a special webpage authorization access_token, And basic support access_token( The access_token Used to call other interfaces ) Different .
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("---------- Wechat exchange openid The result returned :{}----------");
Gson gson = new Gson();
WechatOpenId wechatOpenId = gson.fromJson(jsonStr, WechatOpenId.class);
if (null != wechatOpenId) {
openid = wechatOpenId.getOpenid();
}
} catch (Exception e) {
System.out.println("---------- Wechat exchange openid Something is wrong :{}----------");
}
return openid;
}边栏推荐
- 市场监管总局、国家网信办:开展数据安全管理认证工作
- Such a change in people's understanding of the industrial Internet is not achieved overnight
- Matlab 基础04 - 冒号Colon operator “:”的使用和复杂应用详析
- 中创专利|中国5G标准必要专利达1.8万项,尊重知识产权,共建知识产权强国
- 一文get,最容易碰上的接口自动化测试问题汇总
- Lambda中间操作distinct
- Inventory: more than 20 typical safety incidents occurred in February, with a loss of nearly $400million
- Lambda中间操作sorted
- Jmeter接口测试之常用断言
- Lambda中间操作map
猜你喜欢

Data visualization big screen - big screen cloud minimalist user manual

河南中创|从云到边,边缘计算如何赋能数据中心

100 deep learning cases | day 41: speech recognition - pytorch implementation

Chapter V - Fund professional ethics

Weekly CTF 第一周:神奇的磁带

I worked as a software testing engineer in a large factory and wrote "one day's complete workflow"

VM tools fail in VMware? Install VM tools using Ali image source

MS-HGAT: 基于记忆增强序列超图注意力网络的信息扩散预测

Component introduction - large screen cloud minimalist user manual

How to guarantee industrial control safety: system reinforcement
随机推荐
Lambda创建流
ARP instruction
Henan Zhongchuang - from cloud to edge, how edge computing enables data centers
Argodb 3.2 of star ring technology was officially released to comprehensively upgrade ease of use, performance and security
Equipment encryption of industrial control security
Nat. Comm. | 超算+AI: 为天然产物生物合成路线规划提供导航
[answer] business use cases and system use cases are mixed together
Article 5: Design of multi-functional intelligent trunk following control system | undergraduate graduation design - [learning of controller arduino+stm32]
Elegant throttling / de buffeting decorator under LAYA
Lambda intermediate operation limit
Lambda quick start
VsCode - 保存文件自动格式化将单引号 ‘ 变成双引号 “ 的问题
How to optimize plantuml flow diagram (sequence diagram)
Recurrent+Transformer 视频恢复领域的‘德艺双馨’
How to optimize the opening speed of website pages to improve the experience?
写代码复现论文的几点建议!
只会基础的功能测试的我,被炒鱿鱼了····
一看就懂的JMeter操作流程
About MySQL password modification failure
手写MapReduce程序详细操作步骤