收藏
回答

发送模板消息,返回错误码40001




按照官网的做法,也是2小时才获取一次,否则返回缓存中的数据,但是一直返回40001,一次也没成功过。

package com.stamp.service;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.stamp.util.HttpClientUtil;
import com.stamp.util.JsonHelper;
@Service
public class WxService {
   private static Log log;
   static {
       log = LogFactory.getLog(WxService.class);
   }
   
   private String access_token=""; //内部禁止使用this.access_token获取该字段,微信规定这个字段有过期时间
   private int expires_in=0;       //从微信接口中获取凭证有效时间,单位为秒
   private long refreshSeconds=0;  //调用接口的时间,单位为毫秒
   
   private void refreshAccessToken() throws NumberFormatException, JsonProcessingException, IOException{
       String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"
               + "&appid=APPID&secret=SECRET";
       //String wxInfoJsonStr = HttpClientUtil.getInstance().sendHttpsGet(url);
       try {
           URL getUrl=new URL(url);
           HttpURLConnection http=(HttpURLConnection)getUrl.openConnection();
           http.setRequestMethod("GET");
           http.setRequestProperty("Content-Type",
           "application/x-www-form-urlencoded");
           http.setDoOutput(true);
           http.setDoInput(true);
           http.connect();
           InputStream is = http.getInputStream();
           int size = is.available();
           byte[] b = new byte[size];
           is.read(b);
           String message = new String(b, "UTF-8");
           this.access_token = JsonHelper.readValueFromJsonStr(message, "access_token");
           this.expires_in = Integer.parseInt(JsonHelper.readValueFromJsonStr(message, "expires_in"));
           this.refreshSeconds = new Date().getTime();
           } catch (MalformedURLException e) {
           e.printStackTrace();
           } catch (IOException e) {
           e.printStackTrace();
           }
//          this.access_token = JsonHelper.readValueFromJsonStr(wxInfoJsonStr, "access_token");
//          this.expires_in = Integer.parseInt(JsonHelper.readValueFromJsonStr(wxInfoJsonStr, "expires_in"));
//          this.refreshSeconds = new Date().getTime();
//          log.info(wxInfoJsonStr);
//          log.info("access_token:"+this.access_token);
//          log.info("expires_in:"+this.expires_in);
//          log.info("refreshSeconds:"+this.refreshSeconds);
   }
   
   public String getAccessToken() throws Exception{
       log.info("getAccessToken:"+this.access_token);
       long currentTime = new Date().getTime();
       if(currentTime-refreshSeconds<expires_in*<expires_in*< code="">1000){</expires_in*<>
           return this.access_token;
       }else{
           log.info("refresh access token");
           this.refreshAccessToken();
           return this.access_token;
       }
   }
   
   //主动推动模板消息
   public boolean SenderMessages(String openid,String orderid,String formid,String data){
       try {
           String token = this.getAccessToken();
           
           openid="oaGby0H0539XO7QS5VUNtR3ascGw";
           formid="1505179251686";
           orderid="49c42b16-5a0-2060-33bf-05fa29596732";
           data="";
           log.info("token:"+token);
           log.info("openid:"+openid);
           log.info("orderid:"+orderid);
           log.info("formid:"+formid);
           
           String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token="+token;
           
           Mapmap =new HashMap<>();
           map.put("touser", openid);
           map.put("template_id", "iqMXKd5hXu3nYYLy72LIdg96i09D9pndKBAU7zEn_Og");
           map.put("page", "order?orderid="+orderid);
           map.put("formId", formid);
           map.put("data", data);
           
           String wxInfo = HttpClientUtil.getInstance().sendHttpsPost(url, map);
           log.info(wxInfo);
           return true;
       } catch (Exception e) {
           e.printStackTrace();
       }
       return false;
   }
}


最后一次编辑于  2017-09-13
回答关注问题邀请回答
收藏

1 个回答

  • 2018-03-02

    我获取出来的也没有引号啊,怎么还是40001?求解

    2018-03-02
    有用
    回复
登录 后发表内容