微信学习_06_access_token的获取
来源:互联网 发布:黄鸣龙 知乎 编辑:程序博客网 时间:2024/06/07 01:02
access_token官方文档
点击这里打开access_token官方文档
什么是access_token
access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的access_token失效。
接口调用请求说明
http请求方式: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
参数说明
类型转换,导入JAR包
这里因为需要把其他类型转成JSONObject,需要导入以下JAR包,详情可参见JSON与JavaBean之间互转
commons-beanutils-1.7.0.jar
commons-collections-3.2.1.jar
commons-lang-2.5.jar
commons-logging-1.0.4.jar
ezmorph-1.0.6.jar
json-lib-2.3-jdk15.jar
这里因为需要在后台发起请求获取response结果,所以需要用到DefaultHttpClient中的方法,需导进以下JAR包
httpclient-4.2.5.jar
httpcore-4.2.4
项目代码结构如下
创建WeixinUtil
package com.imooc.util;import java.io.IOException;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.ParseException;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 = "wxcd2e478a611d53cc"; private static final String APPSECRET = "8452eea12aad81a70eeb9470cfb2d751"; 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 * @throws ParseException * @throws IOException */ public static JSONObject doGetStr(String url) throws ParseException, IOException{ DefaultHttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); JSONObject jsonObject = null; HttpResponse httpResponse = client.execute(httpGet); HttpEntity entity = httpResponse.getEntity(); if(entity != null){ String result = EntityUtils.toString(entity,"UTF-8"); jsonObject = JSONObject.fromObject(result); } return jsonObject; } /** * POST请求 * @param url * @param outStr * @return * @throws ParseException * @throws IOException */ public static JSONObject doPostStr(String url,String outStr) throws ParseException, IOException{ DefaultHttpClient client = new DefaultHttpClient(); HttpPost httpost = new HttpPost(url); JSONObject jsonObject = null; httpost.setEntity(new StringEntity(outStr,"UTF-8")); HttpResponse response = client.execute(httpost); String result = EntityUtils.toString(response.getEntity(),"UTF-8"); jsonObject = JSONObject.fromObject(result); return jsonObject; } /** * 获取accessToken * @return * @throws ParseException * @throws IOException */ public static AccessToken getAccessToken() throws ParseException, IOException{ AccessToken token = new AccessToken(); String url = ACCESS_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET); JSONObject jsonObject = doGetStr(url); if(jsonObject!=null){ token.setToken(jsonObject.getString("access_token")); token.setExpiresIn(jsonObject.getInt("expires_in")); } return 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; }}
创建包com.imooc.test,在包中创建测试类WeixinTest
package com.imooc.test;import com.imooc.po.AccessToken;import com.imooc.util.WeixinUtil;public class WeixinTest { public static void main(String[] args) { try { AccessToken token = WeixinUtil.getAccessToken(); System.out.println("票据"+token.getToken()); System.out.println("有效时间"+token.getExpiresIn()); } catch (Exception e) { e.printStackTrace(); } }}
测试
运行WeixinTest,得到票据和有效时间,测试通过
0 0
- 微信学习_06_access_token的获取
- 关于微信公众号获取token的学习总结
- 微信学习系列之一:定时获取微信access_token的线程
- 微信公众平台测试账号的获取及开发使用【微信开发学习】
- 微信openId的获取
- 【微信公众号开发】自我学习第五章:Curl的基础 / accessToken获取 / 微信服务器IP获取
- 微信平台开发获取微信用户的OPENID
- 【微信开发】获取微信头像的问题
- 微信开发的access_token的获取
- 获取微信用户的openId
- 微信登录的code获取
- php获取微信用户的openid
- 微信获取用户的经纬度
- 微信API获取坐标的JS
- 获取微信二维码的 ticket
- 微信access_token的获取开发示例
- 微信开发获取AccessToken的方式
- 获取微信的图片功能
- Failed to load class "org.slf4j.impl.StaticLoggerBinder问题故障的解决
- WEB开发的套路——WEB工程的套路
- Android Studio真机调试时提示the selected device is incompatible
- 每日一练-----返回乘积最大的子数组的积
- Java集合的Stack、Queue、Map的遍历
- 微信学习_06_access_token的获取
- Java汉字转拼音pinyin4j用法
- An internal error occurred during: "Map/Reduce location status updater". java.lang.NullPointerExcept
- 11.常见文件类型的图标介绍 - IntelliJ IDEA 使用教程
- Apache Maven项目提供的EAR插件详解
- 小知识——Butter Knife
- 使用IntelliJ IDEA开发Java Web项目时,修改了JSP后刷新浏览器无法及时显示修改后的页面
- 文章标题
- C#获取文件夹下所有的子文件