微信公众号开发基础篇(七)

一、获取access_token

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

二、返回参数

参数说明
access_token 获取到的凭证
expires_in 凭证有效时间,单位:秒

三、接口的对接

参数是否必须说明
grant_type 获取access_token填写client_credential(一个字符串)
appid 第三方用户唯一凭证
secret 第三方用户唯一凭证密钥,即appsecret

需要调用的接口

             http请求方式: GET

                          https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

上边的参数在公众号的基础设置中获取

公众号的接口分为两类一个是GET请求一个是POST请求,微信公众号的GET接口是从后来获取数据,而POST请求是我们这里向微信公众平台传数据。

比如菜单功能就是用POST上传一个POST请求创建菜单

新版的还需要设置IP白名单,点开后,通过浏览器访问http://ip.qq.com/,可查看当前IP地址。

 将本地的IP地址存储在IP白名单中,然后,通过管理员身份确认。

三、开发实战

      1、导入Jar包

                

 

           2、建立一个方法WeixinUtil

               (1)常量接口

1 // 用户唯一凭证
2     private static final String APPID = "wx7e345608acc1f38b";
3 
4     // 公众平台生成的唯一凭证(需要微信管理员身份在基础设置中设置)
5     private static final String APPSECRET = "e1212aa31566866359f4ac0fe5ae4876";

                (2)设置接收和发送的方法get和post

 1 // 封装获取参数数据的doget方法
 2     public static JSONObject doGetStr(String url) throws ParseException,
 3             IOException {
 4 
 5         // 通过这个参数获取数据
 6         DefaultHttpClient httpclient = new DefaultHttpClient();
 7 
 8         // 通过这个获取url
 9         HttpGet httpGet = new HttpGet(url);
10 
11         // 创建变量用来接收结果
12         JSONObject jsonObject = null;
13 
14         // 接收接口执行的结果
15         HttpResponse httpResponse = httpclient.execute(httpGet);
16 
17         // 从消息体里拿结果
18         HttpEntity entity = httpResponse.getEntity();
19 
20         // 接收的结果不为空
21         if (entity != null) {
22             // 编码的设置,防止乱码
23             String result = EntityUtils.toString(entity, "UTF-8");
24             // 字符串类型转换json格式
25             jsonObject = JSONObject.fromObject(result);
26         }
27         return jsonObject;
28     }
29 
30     // 封装传递数据的post方法
31     public static JSONObject doPostStr(String url, String outStr)
32             throws ParseException, IOException {
33 
34         // 通过这个参数获取数据
35         DefaultHttpClient client = new DefaultHttpClient();
36 
37         // 把我们的url传递进去
38         HttpPost httpost = new HttpPost(url);
39 
40         // 创建变量用来接收结果
41         JSONObject jsonObject = null;
42 
43         // 传递的参数提交上去(设置编码格式)
44         httpost.setEntity(new StringEntity(outStr, "UTF-8"));
45 
46         // 传递到消息体中
47         HttpResponse response = client.execute(httpost);
48 
49         // 接收的结果编码转换
50         String result = EntityUtils.toString(response.getEntity(), "UTF-8");
51 
52         // 通过json接收结果
53         jsonObject = JSONObject.fromObject(result);
54 
55         return jsonObject;
56     }

                 (3)获取accee_token的接口常量

1 // 获取唯一接口调用凭据
2     private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";

                 (4)创建实体类

 1 package com.my.pojo;
 2 
 3 /**
 4  * 唯一接口凭证的实体类
 5  * 
 6  * @author liuya
 7  * 
 8  */
 9 
10 public class AccessToken {
11 
12     // 获取到的凭证
13     private String token;
14     // 凭证有效时间,单位:秒
15     private int expiresIn;
16 
17     public AccessToken() {
18     }
19 
20     public AccessToken(String token, int expiresIn) {
21         this.token = token;
22         this.expiresIn = expiresIn;
23     }
24 
25     public String getToken() {
26         return this.token;
27     }
28 
29     public void setToken(String token) {
30         this.token = token;
31     }
32 
33     public int getExpiresIn() {
34         return this.expiresIn;
35     }
36 
37     public void setExpiresIn(int expiresIn) {
38         this.expiresIn = expiresIn;
39     }
40 
41     @Override
42     public String toString() {
43         return "AccessToken [token=" + token + ", expiresIn=" + expiresIn + "]";
44     }
45 
46 }

                (5)建立操作的方法

 1 // 获取唯一凭证的方法
 2     public static AccessToken getAccessToken() throws ParseException,
 3             IOException {
 4         AccessToken token = new AccessToken();
 5 
 6         // 传递数据
 7         String url = ACCESS_TOKEN_URL.replace("APPID", APPID).replace(
 8                 "APPSECRET", APPSECRET);
 9         // 获取结果
10         JSONObject jsonObject = doGetStr(url);
11 
12         // 对结果进行判断
13         if (jsonObject != null) {
14             // 放入access对象中
15             // 获取到的凭证
16             token.setToken(jsonObject.getString("access_token"));
17             // 时间
18             token.setExpiresIn(jsonObject.getInt("expires_in"));
19         }
20         return token;
21     }

                   (6)书写测试类

1 // 测试access_token的测试类
2     @Test
3     public void test() throws Exception {
4         // 每次获取后存储,否则会过期,每天有次数限制
5         AccessToken accessToken = WeixinUtil.getAccessToken();
6         System.out.println("票据信息 : " + accessToken.getToken());
7         System.out.println("有效时间 : " + accessToken.getExpiresIn());
8 }
原文地址:https://www.cnblogs.com/liuyangfirst/p/7975873.html