微信小程序模板消息遇到的问题

传参不正确

springmvc使用HttpClient向微信服务器发送post请求

返回    errcode: 41001, errmsg: "access_token missing hint: [V.Ee6a07688721!]"

最开始我以为access_token可以和其他参数一起封装了传过去,就总是不成功。后来发现url格式是

正确做法:

微信端传参数

服务器端

 

若不用服务器,直接在小程序里发起请求

 

实践是检验真理得唯一标准

实际操作中发现像access_token还有openid都不能从小程序这边取,因为合法域名的问题。然后关于form_id也是要和用户对应,且只能用一次,时效7天,所以无法实现A点击提交表单得到form_id然后再去给B作推送。但是不管怎样,还是有必要记录一下一些过程,以便后面要再作推送时方便查阅

access_token对象

//微信小程序access_token
public class AccessToken {
	private String accessToken;//token
	private Long expiresTime;//过期时间 时间戳
	
	public AccessToken(){
		
	}
	
	public AccessToken(String accessToken,Integer expiresIn){
		super();
		this.accessToken = accessToken;
		this.expiresTime = System.currentTimeMillis()+expiresIn*1000;
	}

	public String getAccessToken() {
		return accessToken;
	}
	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}
	public Long getExpiresTime() {
		return expiresTime;
	}
	public void setExpiresTime(Long expiresTime) {
		this.expiresTime = expiresTime;
	}
	
	/**
	 * 判断当前token是否过期
	 * @return
	 */
	public Boolean isExpired(){
		return System.currentTimeMillis()>expiresTime;
	}
}

controller

private void getToken(){
			String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type="+"client_credential"+"&appid="+appId+"&secret="+appSecret;
			CloseableHttpClient httpclient = HttpClients.createDefault();
			HttpGet httpget = new HttpGet(url);
			try {
				HttpResponse response = httpclient.execute(httpget);
				int statusCode = response.getStatusLine().getStatusCode();

				if (statusCode >= 200 && statusCode < 300) {
					HttpEntity entity = response.getEntity();
					if (entity != null) {
						JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(entity));
						at=new AccessToken(jsonObject.getString("access_token"),jsonObject.getInteger("expires_in"));
					}
				} else {
					throw new ClientProtocolException("Unexpected response status: " + statusCode);
				}
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}

模板消息构造参考

https://blog.csdn.net/weixin_39984161/article/details/81189684

 

post请求

// 发送post请求
	private HttpResponse requestByPostMethod(String url, String jsonObj) {
		HttpResponse response = null;
		CloseableHttpClient httpclient = HttpClients.createDefault();
		HttpPost post = new HttpPost(url);
		if (jsonObj != null) {
			StringEntity entity = new StringEntity(jsonObj, Charset.forName("UTF-8"));

			entity.setContentEncoding("UTF-8");
			// 发送Json格式的数据请求
			entity.setContentType("application/json");
			post.setEntity(entity);
		}
		try {
			response = httpclient.execute(post);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return response;
	}

参数构造参考那个网址

发送模板消息(HttpClient),调用上面post方法传入参数与网址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值