Java获取Token,含Authorization参数以及body参数
程序员文章站
2022-06-13 19:19:42
...
Java获取Token,含Authorization参数以及body参数
代码如下:
public String getAccesstoken () { String result = null; //请求地址 Properties pro = getProperties(); String url = pro.getProperty("getToken"); String responseInfo = ""; String appId= ""; String appSecret = ""; //body中的参数 String grant_type = ""; String scope=""; //参数设置 List<NameValuePair> parameForToken = new ArrayList<NameValuePair>(); parameForToken.add(new BasicNameValuePair("grant_type", grant_type)); parameForToken.add(new BasicNameValuePair("scope", scope));
//使用base64进行加密 byte[] tokenByte = Base64.encodeBase64((appId+":"+appSecret).getBytes()); //将加密的信息转换为string String tokenStr = Base.bytesSub2String(tokenByte, 0, tokenByte.length); //Basic YFUDIBGDJHFK78HFJDHF== token的格式 String token = "Basic "+tokenStr; // 获取httpclient CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = null; try { //创建post请求 HttpPost httpPost = new HttpPost(url); //PostMethod pMethod = new PostMethod(url); // 设置请求和传输超时时间 RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(20000).setConnectTimeout(20000).build(); httpPost.setConfig(requestConfig); // 提交参数发送请求 UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(parameForToken); httpPost.setEntity(urlEncodedFormEntity); //把认证信息发到header中 httpPost.setHeader("Authorization",token); response = httpclient.execute(httpPost); // 得到响应信息 int statusCode = response.getStatusLine().getStatusCode(); // 判断响应信息是否正确 if (statusCode != HttpStatus.SC_OK) { // 终止并抛出异常 httpPost.abort(); throw new RuntimeException("HttpClient,error status code :" + statusCode); } HttpEntity entity = response.getEntity(); if (entity != null) { //result = EntityUtils.toString(entity);//不进行编码设置 result = EntityUtils.toString(entity, "UTF-8"); } EntityUtils.consume(entity); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { //关闭所有资源连接 if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } if (httpclient != null) { try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println(result); return result; }
- 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
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
getProperties()方法:
public Properties getProperties() {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("resources/constant.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e1) {
System.out.println("读取constant.properties文件失败"+ e1);
e1.printStackTrace();
}
return p;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
Base.java中的bytesSub2String()方法:
/*** * @input src 待截取字节数组 * start 待截取字节数组的开始位置 * src_size 截取的长度 == 数据类型的长度 * * @output String 字节截取转换成String后的结果 * * **/ public static String bytesSub2String(byte[] src,int start,int src_size) { byte[] resBytes = new byte[src_size]; System.arraycopy(src, start, resBytes, 0, src_size); // System.out.println(" len ==" +resBytes.length // + " sub_bytes = " + bytes2Int1(resBytes)); return bytes2String(resBytes); }
// byte[] --> String public static String bytes2String(byte b[]){ String result_str = new String(b); return result_str; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
常量全在constant.properties中写着。接下来一般都是把这个写入定时任务中,因为token一般都有有效时间,过了有效时间需要重新申请。所以写入定时任务中,让定时任务执行并获取token。
借鉴:https://blog.csdn.net/mhqyr422/article/details/79787518
http://chenliang1234576.iteye.com/blog/1167833
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
<div class="more-toolbox">
<div class="left-toolbox">
<ul class="toolbox-list">
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#csdnc-thumbsup"></use>
</svg><span class="name">点赞</span>
<span class="count"></span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-Collection-G"></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-fenxiang"></use>
</svg>分享</a></li>
<!--打赏开始-->
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/qq_29824445">
<img src="https://profile.csdnimg.cn/9/8/A/3_qq_29824445" class="avatar_pic" username="qq_29824445">
<img src="https://g.csdnimg.cn/static/user-reg-year/2x/5.png" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="https://blog.csdn.net/qq_29824445" data-report-click="{"mod":"popu_379"}" target="_blank">我的名字叫张墨迹</a></span>
</div>
<div class="text"><span>发布了5 篇原创文章</span> · <span>获赞 0</span> · <span>访问量 5026</span></div>
</div>
<div class="right-message">
<a href="https://im.csdn.net/im/main.html?userName=qq_29824445" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a>
</div>
</div>
</div>
上一篇: Nginx SSI指令配置详解
下一篇: 夏季如何快速减肥 推荐七种水果减肥法