随笔 - 144  文章 - 0  评论 - 2  阅读 - 91402

获取Token接口

get方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public String getToken() {
        try {
            // 获取token路径
            String tokenUrl = CodeRepositoryUtil.getSysConfigValue("identity.token");
            // 请求路径
            URIBuilder uriBuilder = new URIBuilder(tokenUrl);
            // 参数
            uriBuilder.addParameter("client_id", clientId);
            uriBuilder.addParameter("client_secret", clientSecret);
            // 创建一个连接
            CloseableHttpClient httpclient = HttpClients.createDefault();
            // get请求
            HttpGet get = new HttpGet(uriBuilder.build());
            // 执行请求
            CloseableHttpResponse Response = httpclient.execute(get);
            // 获取相应数据
            HttpEntity entity = Response.getEntity();
            // 转化字符
            String jsonStr = EntityUtils.toString(entity, "UTF-8");
            // 关闭请求
            Response.close();
            return JSONObject.parseObject(jsonStr).getString("accessToken");
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
 
        return null;
    }

  POST方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public String getTokenNew() {
        // 获取token路径
        String tokenUrl ="";// CodeRepositoryUtil.getSysConfigValue("identity.token");
        try {
            // 请求路径
            Map<String,String> map = new HashMap<>();
            URIBuilder uriBuilder = new URIBuilder(tokenUrl);
            // 参数
            map.put("client_id", client_id);
            map.put("client_secret", client_secret);
            map.put("grant_type", grant_type);
            map.put("resource_provider_id", resource_provider_id);
            map.put("Content-Type", "application/json");
            HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 15000);
            HttpPost httppost = new HttpPost(tokenUrl);
            httppost.addHeader("Content-type","application/json; charset=utf-8");
 
            StringEntity entity = new StringEntity(JSONObject.toJSONString(map).toString());
            httppost.setEntity(entity);
            HttpResponse resp = httpclient.execute(httppost);
            // 获取相应数据
            HttpEntity result = resp.getEntity();
            // 转化字符
            String jsonStr = EntityUtils.toString(result, "UTF-8");
            // 关闭请求
            httpclient.getConnectionManager().shutdown();;
            return JSONObject.parseObject(jsonStr).getString("access_token");
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }

  

posted on   IT-QI  阅读(685)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示