Java-微信开发-基于测试账号开发-通过授权获取code,给指定人推送模板消息

2 篇文章 0 订阅

1、内网穿透

由于本人开发的时候访问不到自己的电脑所以做了内网穿透(根据自己的实际情况来),推荐一款内网穿透的工具:
内网穿透(NATAAP)

2、申请测试账号

推荐先观看微信官方文档:
微信公众平台技术文档
(1)、
在这里插入图片描述
(2)、用微信登陆
在这里插入图片描述
登陆之后是这样的
在这里插入图片描述

3、配置-授权回调页面域名:

在微信测试账号页面下拉找到网页服务-网页账号-点击修改
在这里插入图片描述
由于本人填写IP没有调用成功,所以我这里填写的是内网穿透过后产生的外网地址
在这里插入图片描述
注意:一定不要带http或https(具体的不要问为啥,我也不知道,带上之后会访问不到,本人亲测)

4、代码部分(删减过后的)

先写死一个openId发送一条模板消息
–发送方法


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eplugger.weixin.model.TemplateData;
import com.eplugger.weixin.model.WechatTemplate;

import net.sf.json.JSONObject;

public class sendMessage {
	Logger log = LoggerFactory.getLogger(sendMessage.class);

	public static void main(String[] args) {
		try {
			new sendMessage().registTemplate("oeUaUs95C7WJx5cKaXu22qHiscnI");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 用户注册成功的模板消息
	 * 
	 * @throws Exception
	 */
	public void registTemplate(String openId) throws Exception {
		// 获取基础支持的access_token
		String access_token = GetAccessToken.getAccessToken();
		// 发送模板消息
		String resultUrl2 = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
		// 封装基础数据
		WechatTemplate wechatTemplate = new WechatTemplate();
		wechatTemplate.setTemplate_id("XOTXTySg6ytA2HLYmXCCb5SsjWc1og0bsiHbhagGeyc");
		wechatTemplate.setTouser(openId);
		wechatTemplate.setUrl(
				"https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=redirect_uri?response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect");
		Map<String, TemplateData> mapdata = new HashMap<String, TemplateData>();
		// 封装模板数据
		TemplateData first = new TemplateData();
		first.setValue("您好,测试信息。");
		first.setColor("#173177");
		mapdata.put("first", first);

		TemplateData keyword1 = new TemplateData();
		keyword1.setValue("张三");
		first.setColor("#173177");
		mapdata.put("keyword1", keyword1);

		TemplateData keyword2 = new TemplateData();
		keyword2.setValue("通过");
		first.setColor("#173177");
		mapdata.put("keyword2", keyword2);

		TemplateData keyword3 = new TemplateData();
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
		keyword3.setValue(dateFormat.format(date));
		first.setColor("#173177");
		mapdata.put("keyword3", keyword3);

		wechatTemplate.setData(mapdata);
		String jsonString = JSONObject.fromObject(wechatTemplate).toString();
		String json2 = WeiXinUtlis.sendPost(resultUrl2, jsonString);
		JSONObject jsonObject = JSONObject.fromObject(json2);
		int result = 0;
		if (null != jsonObject) {
			if (0 != jsonObject.getInt("errcode")) {
				result = jsonObject.getInt("errcode");
				log.error("错误 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
			}
		}
		log.info("模板消息发送结果:" + result);
	}
}

发送模板地址:除了appid和redirect_uri这两个参数其他的可以写死不用修改

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=redirect_uri&response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect"
redirect_uri:
内网穿透的地址或IP+你要访问的路径,下面是我的访问路径
http://bctnh7.natappfree.cc/weixn/weixinServlet

APPID是你自己测试号的appId
redirect_uri:这个参数就是当你点击消息的时候给这个访问连接重定向到你自己填写的路径下,会自动带上code

–模板类

package com.eplugger.weixin.model;

public class TemplateData {
    private String value;
    private String color;
    public String getValue() {
        return value;
    }
 
    public void setValue(String value) {
        this.value = value;
    }
 
    public String getColor() {
        return color;
    }
 
    public void setColor(String color) {
        this.color = color;
    }
}

–获取code根据code获取到具体的某一个openId

public class WeixinServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String code = request.getParameter("code");// 前端传来的code值
		String openId = getOpenId.getOpenId(code);
	}
}
/**
	 * 获取微信用户的openId
	 * 
	 * @param request
	 * @return
	 */
	public static String getOpenId(String code) {
		System.out.println("code:" + code);
		// 通过code获取页面的access_token,返回的json中也就会有openid了。
		String openIdURL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + APPID
				+ "&secret=" + APPSECRET + "&code=" + code + "&grant_type=authorization_code";
		String openIdJson = WeiXinUtlis.sendGet(openIdURL);
		String openid = (String) JSONObject.fromObject(openIdJson).get("openid");
		return openid;
	}

APPSECRET :跟你的openid在一个地方
在这里插入图片描述
最后可以根据这个具体的openid来调用发送模板消息的方法发送给具体的莫一个人;

本片文章只用于测试,正式开发中可以把授权的路径写在自定义菜单的链接里面;

微信公众号开发-获取openId及用openId获取用户信息

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值