本文转自http://www.cnblogs.com/always-online/category/598553.html

一、微信获取access_token接口简介

  1、请求:该请求是GET方式请求,所以要携带的参数都是附加到url后面传递给微信服务器。请求的url格式如下:

    https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
    其中,APPID与APPSECRET都我们开发的时候自己帐号申请的。
  2、响应:返回数据都是json数据,格式如下:
   正确的时候返回的数据: {"access_token":"ACCESS_TOKEN","expires_in":7200}

     ACCESS_TOKEN:访问token,expires_in为过期时间
   错误的时候返回的数据: {"errcode":40013,"errmsg":"invalid appid"}

     errcode,为错误代码,errmsg为错误信息
   具体api说明可查看文档:http://mp.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96access_token 。

二、关于java代码的调用

  该接口可以在前台用页面ajax调用,也可以在后台用java代码调用。这里需要使用到apache的http组件httpcomponents-client,这里使用的版本为httpcomponents-client-4.2.1,下载地址为:http://hc.apache.org/downloads.cgi。需要使用到的jar文件如下:

三、代码实现

 1 package com.demo.test;
 2
 3 import org.apache.http.HttpEntity;
 4 import org.apache.http.HttpResponse;
 5 import org.apache.http.HttpStatus;
 6 import org.apache.http.client.HttpClient;
 7 import org.apache.http.client.methods.HttpGet;
 8 import org.apache.http.impl.client.DefaultHttpClient;
 9 import org.apache.http.util.EntityUtils;
10
11 import com.google.gson.JsonObject;
12 import com.google.gson.JsonParser;
13
14 public class Test
15 {
16     public static final String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";// 获取access
17                                                                                             // url
18     public static final String APP_ID = "wxa549b28c24cf341e";
19     public static final String SECRET = "78d8a8cd7a4fa700142d06b96bf44a37";
20
21     // 获取token
22     public static String getToken(String apiurl, String appid, String secret)
23     {
24         String turl = String.format(
25                 "%s?grant_type=client_credential&appid=%s&secret=%s", apiurl,
26                 appid, secret);
27         HttpClient client = new DefaultHttpClient();
28         HttpGet get = new HttpGet(turl);
29         JsonParser jsonparer = new JsonParser();// 初始化解析json格式的对象
30         String result = null;
31         try
32         {
33             HttpResponse res = client.execute(get);
34             String responseContent = null; // 响应内容
35             HttpEntity entity = res.getEntity();
36             responseContent = EntityUtils.toString(entity, "UTF-8");
37             JsonObject json = jsonparer.parse(responseContent)
38                     .getAsJsonObject();
39             // 将json字符串转换为json对象
40             if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
41             {
42                 if (json.get("errcode") != null)
43                 {// 错误时微信会返回错误码等信息,{"errcode":40013,"errmsg":"invalid appid"}
44                 }
45                 else
46                 {// 正常情况下{"access_token":"ACCESS_TOKEN","expires_in":7200}
47                     result = json.get("access_token").getAsString();
48                 }
49             }
50         }
51         catch (Exception e)
52         {
53             e.printStackTrace();
54         }
55         finally
56         {
57             // 关闭连接 ,释放资源
58             client.getConnectionManager().shutdown();
59             return result;
60         }
61     }
62
63     public static void main(String[] args) throws Exception
64     {
65         System.out.println("=========1获取token=========");
66         String accessToken = getToken(GET_TOKEN_URL, APP_ID, SECRET);// 获取token
67         if (accessToken != null)
68             System.out.println(accessToken);
69     }
70
71 }

  当token正常返回的时候会打印token,否则不会打印。

 
分类: 微信

java微信接口之——获取access_token的更多相关文章

  1. java微信接口之五—消息分组群发

    一.微信消息分组群发接口简介 1.请求:该请求是使用post提交地址为: https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_t ...

  2. java微信接口之四—上传素材

    一.微信上传素材接口简介 1.请求:该请求是使用post提交地址为: https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=A ...

  3. 微信小程序获取Access_token和页面URL生成小程序码或二维码

    1.微信小程序获取Access_token: access_token具体时效看官方文档. using System; using System.Collections.Generic; using ...

  4. 微信公众平台——获取access_token、expires_in

    微信公众平台——获取access_token.expires_in 在微信公众平台接口开发中,Access Token占据着重要地位,它相当于进入各种接口的邀请,拿到这个钥匙才拥有调用其他各种特殊接口 ...

  5. java微信接口之二—获取用户组

    一.微信获取用户组接口简介 1.请求 该请求也是GET方式请求.请求的url格式如下: https://api.weixin.qq.com/cgi-bin/groups/get?access_toke ...

  6. 微信接口开发1--向微信发送请求--获取access_token

    //随便放置一个php文件在服务器上.执行该方法--调用模拟get提交---到微信-->获得微信返回的access_token 不建议自己编写模拟get提交方法. 建议直接导入微信框架LaneW ...

  7. java微信接口之三—上传多媒体文件

    一.微信上传多媒体接口简介 1.请求:该请求是使用post提交from来实现的,我们可以在网页上进行表单提交来实现.地址为: http://file.api.weixin.qq.com/cgi-bin ...

  8. java 微信自定义菜单 java微信接口开发 公众平台 SSM redis shiro 多数据源

    A 调用摄像头拍照,自定义裁剪编辑头像,头像图片色度调节B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,快速开发利器)+快速表单构建器 freemaker模版技术 ,0个代码不用写,生成 ...

  9. 微信公众号 --- 获取access_token

    获取access_token 在左侧菜单栏中也可以找到 可以一步步的进行设置  ,  身份验证的时候要 注意:密码是你创建微信公众号的密码 往一步步的执行就可以了 接下来就是获取ip 白名单,进行设置 ...

随机推荐

  1. <?xml version="1.0" encoding="utf-16"?>. use different encoding

    public string Serialize<T>(T serializeClass) { string xmlString = string.Empty; try { if (seri ...

  2. 微型orm fluentdata

    http://fluentdata.codeplex.com/documentation#Query

  3. tomcat的文件路径 servelet的配置 以及maven中的WEB-INF的路径

    Tomcat JavaWeb应用的组成结构 开发JavaWeb应用时,不同类型的文件有严格的存放规则,否则不仅可能会使web应用无法访问,还会导致web服务器启动报错 WebRoot →Web应用所在 ...

  4. Unlink of file '.git/objects/pack/pack-***.pack' failed. Should I try again? (y/n) (转)

    git pull的时候遇到 Unlink of file '.git/objects/pack/pack-***.pack' failed. Should I try again? (y/n) y 于 ...

  5. PHP 函数整理 (用过的)

    1:$_SERVER['DOCUMENT_ROOT'] $_SERVER['DOCUMENT_ROOT']是PHP预定义的几个变量之一.作用是:获取当前运行脚本所在的文档根目录.该根目录是由服务器配置 ...

  6. c# 获取系统时间

    //获取日期+时间DateTime.Now.ToString();             // 2008-9-4 20:02:10DateTime.Now.ToLocalTime().ToStrin ...

  7. 全屏背景:15个jQuery插件实现全屏背景图像或媒体

    动态网站通常利用背景图像或预加载屏幕,以保证所有资源都加载到页面上,在浏览器中充分呈现.现在很多网站都炫耀自己的图像作为背景图像全屏背景,追溯到旧的Flash网站却用自己的方式在HTML资源重布局. ...

  8. AngularJS API之extend扩展对象

    angular.extend(dst,src),在我实验的1.2.16版本上是支持深拷贝的.但是最新的API显示,这个方法是不支持深拷贝的. 另外,第二个参数src支持多个对象. 第一种使用方式 va ...

  9. Ansible简明使用手册

            Ansible使用简明手册 1.简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric ...

  10. Win7与XP共享互相访问及共享注意事项!

    win7共享方法和XP类似,主要需要检查以下操作: 1,首先将Guest账户打开 2,右击文件夹-属性-共享选项-高级共享 3,将共享文件√打上,应用-确定即可! 4,查看自己IP(开始-运行-cmd ...