微信小程序——Java后台获取access_token

下面就整理了关于如何获取access_token

 现在突然搞清楚,微信小程序,后台不能直接得到用户信息,只有前端才可以得到。

 搞了半天。哎~~

请求地址

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

后台代码 

通过APPID和Secret获取到access_token


import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.util.Map;

public class GetDetailByUrl {
    public static Map<String,Object> getDetailByUrl(String ur) {
        Map<String, Object> map = null;
        String access_token_url = "https://api.weixin.qq.com/cgi-bin/token?" +
                "grant_type=client_credential" +
                "&appid=你的appid"+
                "&secret=你的secret";
        try {
            HttpClient client = HttpClientBuilder.create().build();//构建一个Client
            HttpGet get = new HttpGet(url.toString());    //构建一个GET请求
            HttpResponse response = client.execute(get);//提交GET请求
            HttpEntity result = response.getEntity();//拿到返回的HttpResponse的"实体"
            String content = EntityUtils.toString(result);
            JSONObject res = JSONObject.fromObject(content);
            map = JsonUtil.parseJSON2Map(res);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("获取信息失败");
        }
        return map;
    }
}

通过下面的方法将JSON转化为Map

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.*;
public class JsonUtil {
    public static Map<String, Object> parseJSON2Map(JSONObject json) {
        Map<String, Object> map = new HashMap<String, Object>();
        // 最外层解析
        for (Object k : json.keySet()) {
            Object v = json.get(k);
            // 如果内层还是数组的话,继续解析
            if (v instanceof JSONArray) {
                List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                @SuppressWarnings("unchecked")
                Iterator<JSONObject> it = ((JSONArray) v).iterator();
                while (it.hasNext()) {
                    JSONObject json2 = it.next();
                    list.add(parseJSON2Map(json2));
                }
                map.put(k.toString(), list);
            } else {
                map.put(k.toString(), v);
            }
        }
        return map;
    }
}
//JSON转化为MAP函数中需要
<dependency>
	<groupId>net.sf.json-lib</groupId>
	<artifactId>json-lib</artifactId>
	<version>2.4</version>
	<classifier>jdk15</classifier>
</dependency> 

请求参数

属性 类型 默认值 必填 说明
grant_type string   填写 client_credential
appid string  

小程序唯一凭证,即 AppID,可在「微信公众平台 - 设置 - 开发设置」页中获得。

(需要已经成为开发者,且帐号没有异常状态)

secret string   小程序唯一凭证密钥,即 AppSecret,获取方式同 appid

返回值Object

返回的 JSON 数据包

属性 类型 说明
access_token string 获取到的凭证
expires_in number 凭证有效时间,单位:秒。目前是7200秒之内的值。
errcode number 错误码
errmsg string 错误信息

errcode 的合法值

说明  
-1 系统繁忙,此时请开发者稍候再试  
0 请求成功  
40001 AppSecret 错误或者 AppSecret 不属于这个小程序,请开发者确认 AppSecret 的正确性  
40002 请确保 grant_type 字段值为 client_credential  
40013 不合法的 AppID,请开发者检查 AppID 的正确性,避免异常字符,注意大小写  

返回数据示例

正常返回

{"access_token":"ACCESS_TOKEN","expires_in":7200}

错误时返回

{"errcode":40013,"errmsg":"invalid appid"}
发布了183 篇原创文章 · 获赞 26 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/SEVENY_/article/details/104581189