获取access_token
2018-01-09 09:19
85 查看
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 net.sf.json.JSONObject;
public class WeiXinUtil {
//从微信后台拿到APPID和APPSECRET
private static final String APPID = "APPID";
private static final String APPSECRET = "APPSECRET";
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token? grant_type=client_credential&appid=APPID&secret=APPSECRET";
/**
* 编写Get请求的方法。但没有参数传递的时候,可以使用Get请求
*
* @param url 需要请求的URL
* @return 将请求URL后返回的数据,转为JSON格式,并return
*/
public static JSONObject doGetStr(String url) throws ClientProtocolException, IOException {
DefaultHttpClient client = new DefaultHttpClient();//获取DefaultHttpClient请求
HttpGet httpGet = new HttpGet(url);//HttpGet将使用Get方式发送请求URL
JSONObject jsonObject = null;
HttpResponse response = client.execute(httpGet);//使用HttpResponse接收client执行httpGet的结果
HttpEntity entity = response.getEntity();//从response中获取结果,类型为HttpEntity
if(entity != null){
String result = EntityUtils.toString(entity,"UTF-8");//HttpEntity转为字符串类型
jsonObject = JSONObject.fromObject(result);//字符串类型转为JSON类型
}
return jsonObject;
}
/**
* 编写Post请求的方法。当我们需要参数传递的时候,可以使用Post请求
*
* @param url 需要请求的URL
* @param outStr 需要传递的参数
* @return 将请求URL后返回的数据,转为JSON格式,并return
*/
public static JSONObject doPostStr(String url,String outStr) throws ClientProtocolException, IOException {
DefaultHttpClient client = new DefaultHttpClient();//获取DefaultHttpClient请求
HttpPost httpost = new HttpPost(url);//HttpPost将使用Get方式发送请求URL
JSONObject jsonObject = null;
httpost.setEntity(new StringEntity(outStr,"UTF-8"));//使用setEntity方法,将我们传进来的参数放入请求中
HttpResponse response = client.execute(httpost);//使用HttpResponse接收client执行httpost的结果
String result = EntityUtils.toString(response.getEntity(),"UTF-8");//HttpEntity转为字符串类型
jsonObject = JSONObject.fromObject(result);//字符串类型转为JSON类型
return jsonObject;
}
/**
* 获取AccessToken
* @return 返回拿到的access_token及有效期
*/
public static String getAccessToken() throws ClientProtocolException, IOException{
String url = ACCESS_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET);//将URL中的两个参数替换掉
JSONObject jsonObject = doGetStr(url);//使用刚刚写的doGet方法接收结果
String token = "";
if(jsonObject!=null){ //如果返回不为空,将返回结果封装进AccessToken实体类
token = (jsonObject.getString("access_token"));//取出access_token
// token.setExpiresIn(jsonObject.getInt("expires_in"));//取出access_token的有效期 我这边先注释掉
}
return token;
}
public static void main(String[] args) throws ClientProtocolException, IOException {
String string = WeiXinUtil.getAccessToken();
System.out.println(string);
}
}
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 net.sf.json.JSONObject;
public class WeiXinUtil {
//从微信后台拿到APPID和APPSECRET
private static final String APPID = "APPID";
private static final String APPSECRET = "APPSECRET";
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token? grant_type=client_credential&appid=APPID&secret=APPSECRET";
/**
* 编写Get请求的方法。但没有参数传递的时候,可以使用Get请求
*
* @param url 需要请求的URL
* @return 将请求URL后返回的数据,转为JSON格式,并return
*/
public static JSONObject doGetStr(String url) throws ClientProtocolException, IOException {
DefaultHttpClient client = new DefaultHttpClient();//获取DefaultHttpClient请求
HttpGet httpGet = new HttpGet(url);//HttpGet将使用Get方式发送请求URL
JSONObject jsonObject = null;
HttpResponse response = client.execute(httpGet);//使用HttpResponse接收client执行httpGet的结果
HttpEntity entity = response.getEntity();//从response中获取结果,类型为HttpEntity
if(entity != null){
String result = EntityUtils.toString(entity,"UTF-8");//HttpEntity转为字符串类型
jsonObject = JSONObject.fromObject(result);//字符串类型转为JSON类型
}
return jsonObject;
}
/**
* 编写Post请求的方法。当我们需要参数传递的时候,可以使用Post请求
*
* @param url 需要请求的URL
* @param outStr 需要传递的参数
* @return 将请求URL后返回的数据,转为JSON格式,并return
*/
public static JSONObject doPostStr(String url,String outStr) throws ClientProtocolException, IOException {
DefaultHttpClient client = new DefaultHttpClient();//获取DefaultHttpClient请求
HttpPost httpost = new HttpPost(url);//HttpPost将使用Get方式发送请求URL
JSONObject jsonObject = null;
httpost.setEntity(new StringEntity(outStr,"UTF-8"));//使用setEntity方法,将我们传进来的参数放入请求中
HttpResponse response = client.execute(httpost);//使用HttpResponse接收client执行httpost的结果
String result = EntityUtils.toString(response.getEntity(),"UTF-8");//HttpEntity转为字符串类型
jsonObject = JSONObject.fromObject(result);//字符串类型转为JSON类型
return jsonObject;
}
/**
* 获取AccessToken
* @return 返回拿到的access_token及有效期
*/
public static String getAccessToken() throws ClientProtocolException, IOException{
String url = ACCESS_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET);//将URL中的两个参数替换掉
JSONObject jsonObject = doGetStr(url);//使用刚刚写的doGet方法接收结果
String token = "";
if(jsonObject!=null){ //如果返回不为空,将返回结果封装进AccessToken实体类
token = (jsonObject.getString("access_token"));//取出access_token
// token.setExpiresIn(jsonObject.getInt("expires_in"));//取出access_token的有效期 我这边先注释掉
}
return token;
}
public static void main(String[] args) throws ClientProtocolException, IOException {
String string = WeiXinUtil.getAccessToken();
System.out.println(string);
}
}
相关文章推荐
- 微信获取access_token,access_token有什么意义,他的时长是多少
- 获取微信公众平台access_token
- 百度AI模板 phpCI框架 通过access_token获取 人脸识别信息
- node搭建微信平台(三)-获取access_token
- 微信&java 开发4 access_token获取
- 使用Java进行微信开发3- 获取access_token
- java微信签名,验证微信发送的signature,还有获取access_token和ticket
- android 新浪微博登录获取access_token值的问题
- OpenResty获取微信公众号access_token
- 微信测试号开发之四 获取access_token和jsapi_ticket
- php微信2 获取自定义菜单中的access_token
- 微信支付java版本之获取Access_token
- java微信获取access_token(java微信开发学习笔记4)
- 微信asp+sqlser开发实例——获取access_token
- 微信公众号获取access_token
- 微信公众号平台接口开发:基础支持,获取access_token
- 微信开发使用access_token 获取微信的ip地址
- 微信access_token定期获取并存储
- 微信——获取用户基本信息及openid 、access_token、code
- java开发微信端获取code和opendId,access_token,用户列表