按照官网的做法,也是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="">
|
我获取出来的也没有引号啊,怎么还是40001?求解