接上面几讲内容:
获取Access_token的微信公众平台文档:
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183
要先将自己的ip放入ip白名单中,
参考文章:
IP白名单添加了当前IP,获取access_token时依然报出错误码40164的坑
注意在浏览器中输入URL,URL中替换掉自己的APPID和APPSECRET,直接访问也可测试是否获取access_token
代码测试如下:
AccessToken类:
package com.imooc.po;
public class AccessToken {
private String token;
private int expiresIn;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public int getExpiresIn() {
return expiresIn;
}
public void setExpiresIn(int expiresIn) {
this.expiresIn = expiresIn;
}
}
package com.imooc.util;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import com.imooc.po.AccessToken;
import net.sf.json.JSONObject;
public class WeixinUtil {
private static final String APPID="wxfc73577ca8460519";
private static final String APPSECRET="b0b75d65ccb882f8b5526538b48121cb";
private static final String ACCESS_TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
/**
* get请求
* @param url
* @return
*/
public static JSONObject doGetStr(String url) {
DefaultHttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(url);
JSONObject jsonObject=null;
try {
HttpResponse response=httpClient.execute(httpGet);
HttpEntity entity=response.getEntity();
if(entity!=null) {
String result=EntityUtils.toString(entity,"utf-8");
jsonObject=JSONObject.fromObject(result);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
/**
* post请求
* @param url
* @param outStr
* @return
*/
public static JSONObject doPostStr(String url,String outStr){
DefaultHttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(url);
JSONObject jsonObject=null;
try {
httpPost.setEntity(new StringEntity(outStr,"utf-8"));
HttpResponse response=httpClient.execute(httpPost);
String result=EntityUtils.toString(response.getEntity(),"utf-8");
jsonObject=JSONObject.fromObject(result);
}catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
/**
*
* 获取access_token
* @return
*/
public static AccessToken getAccessToken() {
AccessToken token=new AccessToken();
String url=ACCESS_TOKEN_URL.replace("APPID",APPID ).replace("APPSECRET",APPSECRET);
JSONObject jsonObject= doGetStr(url);
if(jsonObject!=null) {
if(jsonObject.containsKey("access_token")) {
token.setToken(jsonObject.getString("access_token"));
token.setExpiresIn(jsonObject.getInt("expires_in"));
}else {
System.out.println("获取access_token失败");
}
}
return token;
}
}
测试是否获取到微信服务器的AccessToken:
package com.imooc.test;
import com.imooc.po.AccessToken;
import com.imooc.util.WeixinUtil;
public class WeixinTest {
public static void main(String[] args) {
AccessToken token=WeixinUtil.getAccessToken();
System.out.println("票据:"+token.getToken());
System.out.println("有效时间:"+token.getExpiresIn());
}
}
运行结果: