关于微信公众号获取token值和模板推送接口对接问题

今天做了一个关于微信的接口,由于以前没有接触过关于微信的问题,现在碰到了查了很多资料,下面总结一下

1:获取token值,微信获取公众号token值需要公众号的appid和secret 这两个值是微信提供的,是不会变的
获取token值地址
String strUrl = “https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret=”+secret;
通过调用这个借口获取token值。
具体调用方法:
下面是我自己封装的调用借口

public class RpcHttpUtil {
	private static final CloseableHttpClient httpclient = HttpClients.createDefault();
	 /**
     * 发送HttpGet请求
     * @param url
     * @return
     */
    public static String sendGet(String url) {
        HttpGet httpget = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpget);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        String result = null;
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    public static String sendPost(String url) {
        HttpPost httpPost = new  HttpPost(url);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httpPost);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        String result = null;
        try {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

}

直接传入url就可以获取到token,当然微信返回值是json形式,那么我们就需要将token值取出来
具体方法 RpcHttpUtil是我封装的类名
String s = RpcHttpUtil.sendGet(strUrl);
JSONObject jsonObject = JSONObject.parseObject(s);
String access_token = jsonObject.getString(“access_token”);
System.out.println(access_token);
这样就可以取到token值了。
获取到token值我们就可以调用微信的接口了,微信官网上api写的已经很清楚了,下面是我个人的一些总结
首先我们要先创建一个模板,模板可以由微信官网创建,这里就不介绍了,每个公司都有自己的模板,如果是有模板的话直接调用模板id就可以了
调用模板推送接口,下面也是我自己封装的类:

public class TuiSongUtil {
	public static String sendTemplate(String openId,String carnum,String info,String accessToken,String date) {
		String tepUrl = "http://api.weixin.qq.com/cgi-bin/message/template/send?access_token="
				+ accessToken;
		//System.out.println("token="+accessToken);
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(tepUrl);
		// 装配post请求参数
		JSONObject json = new JSONObject();
		json.put("touser", "需要推给某人的opid");
		json.put("template_id", "模板id");
		json.put("url", "需要跳转的ulr(写不写都可以)");
		JSONObject dataJson = new JSONObject();
		TemplateData first = new TemplateData();
		first.setValue("您好,您的爱车"+carnum+ "今日限行");
		first.setColor("#173177");
		dataJson.put("first",first);
		TemplateData keyword1 = new TemplateData();
		keyword1.setValue(info);
		keyword1.setColor("#173177");
		dataJson.put("keyword1",keyword1);
		TemplateData keyword2 = new TemplateData();
		keyword2.setValue(date);
		keyword2.setColor("#173177");
		dataJson.put("keyword2",keyword2);
		TemplateData keyword3 = new TemplateData();
		keyword3.setValue("07:00~19:00");
		keyword3.setColor("#173177");
		dataJson.put("keyword3",keyword3);
		TemplateData keyword4 = new TemplateData();
		keyword4.setValue("请合理安排驾车出行。");
		keyword4.setColor("#173177");
		dataJson.put("keyword4",keyword4);
		TemplateData remark = new TemplateData();
		remark.setValue("提供的数据不能作为您出行的依据,请谨慎参考。");
		remark.setColor("#FF0000");
		dataJson.put("remark",remark);
		net.sf.json.JSONObject json_test = net.sf.json.JSONObject.fromObject(dataJson);
		json.put("data", dataJson);
		String resultStr = "发送失败";
		try {
			StringEntity myEntity = new StringEntity(json.toJSONString(), ContentType.APPLICATION_JSON);

			// 设置post求情参数
			httpPost.setEntity(myEntity);
			HttpResponse httpResponse = httpClient.execute(httpPost);
			System.out.println(httpResponse.getStatusLine().getStatusCode());
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				// 发送成功
				String resutlEntity = EntityUtils.toString(httpResponse.getEntity());
				System.out.println(resutlEntity);
				return resultStr;
			} else {
				// 发送失败
				return resultStr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (httpClient != null) {
					// 释放资源
					httpClient.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return resultStr;
	}
}

下面是创建的实体类

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

这样模板推送就完成了

你可能感兴趣的:(java个人)