连接OAuth2.0

创建一个util类

public static JSONObject doPost(String url , Map<String,Object> paramMap , Map<String,String> headerMap){
        log.info("dopost url = " + url);
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpResponse = null;
        JSONObject jsonObject = null;
        String result = null;
        //创建http实例
        httpClient = HttpClients.createDefault();
        //创建httpPost远程实例
        HttpPost httpPost = new HttpPost(url);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)//设置连接主机超时时间
                                            .setConnectionRequestTimeout(35000)//连接请求超时时间
                                            .setSocketTimeout(60000)//读取数据超时时间
                                            .build();
        httpPost.setConfig(requestConfig);
        if(headerMap == null){
            httpPost.addHeader("Content-Type","application/x-www-form-urlencoded");
        }else{
            for(String key : headerMap.keySet()){
                httpPost.addHeader(key,headerMap.get(key));
            }
        }

        if(paramMap.size()>0 && paramMap != null){
            List list = new ArrayList<>();
            Set<Map.Entry<String, Object>> entrySet = paramMap.entrySet();
            Iterator<Map.Entry<String, Object>> iterator = entrySet.iterator();
            while(iterator.hasNext()){
                Map.Entry<String, Object> next = iterator.next();
                list.add(new BasicNameValuePair(next.getKey(),next.getValue().toString()));
            }
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(list,"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                log.error(e.getMessage(), e);
            }
        }
        try {
            httpResponse = httpClient.execute(httpPost);
            result = EntityUtils.toString(httpResponse.getEntity());
            jsonObject = JSONObject.parseObject(result);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }finally {
            if(httpResponse != null){
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
            if(httpClient != null){
                try {
                    httpClient.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
        return jsonObject;
    }

上述代码中urlAccess TokenurlparamMap是获取token需要的参数。
例:

paramMap.put("grant_type","client_credentials");
paramMap.put("client_id","your client_id");
paramMap.put("client_secret","your client_secret");
paramMap.put("scope","");

将上述参数传入后,得到一个jsonObject类型的返回值
使用jsonObject.get("access_token").toString()获取token
创建一个map里面传入Content-TypeAuthorization如下:

Map headers = new HashMap();
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer "+token);

最后调用HttpUtils提供的post方法,里面参数为连接地址url,传入的参数及上面的map
HttpUtils.post(“target url”, JSON.toJSONString(param) ,headers);
返回值为一个String字符串。
连接完成!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值