获取Token接口
get方式
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方式
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; }