java微信接口之——获取access_token

2019-03-02 01:08|来源: 网路

一、微信获取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,否则不会打印。


转自:http://www.cnblogs.com/always-online/p/3870634

相关问答

更多
  • 我刚做了一个调用接口生成一个二维码 在自己网站内展示,通过微信扫描 二维码支付。
  • 本文实例讲述了PHP定时任务获取微信access_token的方法。分享给大家供大家参考,具体如下: 微信access_token在开发时会变的好像是几分种不一样了,这里我们来介绍关于PHP定时任务获取微信access_token的方法。 最近开发微信公众平台,公众号调用各接口时都需使用access_token,access_token是公众号的全局唯一接口调用凭据,开发时需要进行妥善保存。 access_token有效期为7200秒 ,重复获取将导致上次获取的access_token失效。 由于微信对获取 ...
  • 楼主,想问一下 调用获取微信用户信息的接口是可以随时调用,不依赖于其他条件吗?比如小程序中的获取用户信息的接口调用是不依赖于小程序是否启动吗
  • 要获取访问令牌,您必须完成OAuth 2.0授权流程。 您可以在此处找到有关OAuth 2.0如何工作以及所需步骤的更多详细信息: https://developers.google.com/identity/protocols/OAuth2 To get an access token you're going to have to go through the OAuth 2.0 authorization flow. You can find more details about how OAuth ...
  • 事实证明,截至2007年8月29日,项目中存在一个未公开的问题,即添加此功能,因此它尚不存在。 https://github.com/pennersr/django-allauth/issues/420 It turns out that as of 29/8/16 there is an open issue on the project to add this functionality, so it doesn't exist yet. https://github.com/pennersr/djan ...
  • 如果你使用cURL来调用url,你应该像下面这样调用它 curl -v -D https://org-name.okta.com/oauth2/v1/authorize 如果你使用REST客户端来调用这个URL,那么是的,它会返回重定向URI。 因为这是从浏览器调用的隐式流。 当你从浏览器调用这个URL(JavaScript或复制粘贴URL并按下回车键)时,你将被重定向到redirectUri并且令牌(id令牌和/或访问令牌)将位于重定向应用程序的url中,如下所示。 http:// localhost:/ ...
  • 使用["access_token"]=> "some_other_access_token"这将满足您的需求。 但是你可以在这里阅读完整的文档: https : //developer.linkedin.com/docs/best-practices#keysecret Use ["access_token"]=> "some_other_access_token" this will meet your need. but you can read complete documentation here: ...
  • 通过检查演示应用程序页面获得的public_token是针对该演示应用程序的client_id和secret。 它不会与您的client_id和秘密一起使用。 您需要使用client_id和secret在本地运行演示应用程序。 看看这个 - https://github.com/plaid/quickstart/tree/master/node 按照自述文件在本地运行演示,但使用client_id和secret,然后像上面那样检查页面。 你这次得到的public_token将与你的client_id和秘密: ...
  • 你需要做的第一件事是阅读: https://developers.facebook.com/roadmap/offline-access-removal/ 简短的回答是,您默认收到的令牌将是短暂的(1-2小时)。 Facebook已删除了离线访问权限,但您现在可以将短生命令牌换成长寿令牌(60天)。 您每次访问您的网站时都会更新此令牌以获得另外60天。 此资源可帮助您了解访问令牌过期的原因。 它还记录了当用户出现时检测到过期令牌时要采取的正确步骤。 https://developers.facebook.c ...