Java 微信公众号(二)——获取access_token

通过阅读微信开发者文档会发现——>access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。


在获取access_token时使用的是get请求,那么也就是说我们需要在通过httpclient发送以个get请求

最后返回的是一个json格式

下面直接贴入代码

此时已经可以获取到access_token了,但是还不满足我们的开发使用,因为在微信对于access_token有时间要求,access_token会在两个小时后失效,并且一天只能调用2000次,所以我们需要对代码进行一个封装,下面是真正使用的

[java]  view plain  copy
  1. package com.website.commons.web.utils;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.http.HttpResponse;  
  6. import org.apache.http.HttpStatus;  
  7. import org.apache.http.client.ClientProtocolException;  
  8. import org.apache.http.client.HttpClient;  
  9. import org.apache.http.client.methods.HttpGet;  
  10. import org.apache.http.impl.client.DefaultHttpClient;  
  11. import org.apache.http.util.EntityUtils;  
  12. import org.json.JSONObject;  
  13.   
  14. public class Constant {  
  15.       
  16.     public static final String APPID = "你的APPID";  
  17.       
  18.     public static final String APPSECRET = "你的APPSECRET";  
  19.       
  20.     /**全局token 所有与微信有交互的前提 */  
  21.     public static String ACCESS_TOKEN;  
  22.       
  23.     /**全局token上次获取事件 */  
  24.     public static long LASTTOKENTIME;  
  25.       
  26.     /** 
  27.      * 获取全局token方法 
  28.      * 该方法通过使用HttpClient发送http请求,HttpGet()发送请求 
  29.      * 微信返回的json中access_token是我们的全局token 
  30.      */  
  31.     public static synchronized void getAccess_token(){  
  32.         if(ACCESS_TOKEN == null || System.currentTimeMillis() - LASTTOKENTIME > 7000*1000){  
  33.             try {  
  34.                 //请求access_token地址  
  35.                 String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx9c3336b0bdb29172&secret=e22ea5453c6c10326045a00112c873f4";  
  36.                 //创建提交方式  
  37.                 HttpGet httpGet = new HttpGet(url);  
  38.                 //获取到httpclien  
  39.                 HttpClient httpClient = new DefaultHttpClient();  
  40.                 //发送请求并得到响应  
  41.                 HttpResponse response = httpClient.execute(httpGet);  
  42.                 //判断请求是否成功  
  43.                 if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  
  44.                     //将得到的响应转为String类型  
  45.                     String str = EntityUtils.toString(response.getEntity(), "utf-8");  
  46.                     //字符串转json  
  47.                     JSONObject jsonObject = new JSONObject(str);  
  48.                     //输出access_token  
  49.                     System.out.println((String) jsonObject.get("access_token"));  
  50.                     //给静态变量赋值,获取到access_token  
  51.                     ACCESS_TOKEN = (String) jsonObject.get("access_token");  
  52.                     //给获取access_token时间赋值,方便下此次获取时进行判断  
  53.                     LASTTOKENTIME = System.currentTimeMillis();  
  54.                 }  
  55.             } catch (ClientProtocolException e) {  
  56.                 // TODO Auto-generated catch block  
  57.                 e.printStackTrace();  
  58.             } catch (IOException e) {  
  59.                 // TODO Auto-generated catch block  
  60.                 e.printStackTrace();  
  61.             }  
  62.         }  
  63.     }  
  64.     public static void main(String[] args) {  
  65.         getAccess_token();  
  66.     }  
  67. }  
ok!代码完工,根据大家的编码风格可以做改动

猜你喜欢

转载自blog.csdn.net/weixin_41690905/article/details/80745844