Oauth2AccessToken.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package com.iamberry.nuonuo.oauth;
  2. import com.iamberry.nuonuo.test.TestOpenApiV1;
  3. import com.iamberry.nuonuo.util.StateData;
  4. import net.sf.json.JSONArray;
  5. import net.sf.json.JSONObject;
  6. import org.apache.commons.lang.StringUtils;
  7. import org.apache.http.HttpEntity;
  8. import org.apache.http.client.methods.CloseableHttpResponse;
  9. import org.apache.http.client.methods.HttpUriRequest;
  10. import org.apache.http.client.methods.RequestBuilder;
  11. import org.apache.http.impl.client.CloseableHttpClient;
  12. import org.apache.http.impl.client.HttpClients;
  13. import org.apache.http.util.EntityUtils;
  14. import java.io.IOException;
  15. import java.util.*;
  16. /**
  17. * ISV类用户在获取到auth_code后可套用此方法
  18. * @author sdk.jss.com.cn
  19. * @version 2.0
  20. * @since jdk1.6
  21. **/
  22. public class Oauth2AccessToken {
  23. public static String _url = StateData.accessToken_url; //获取token地址
  24. public static String _header = StateData.contentType; //http发送模式
  25. public static String client_id = StateData.client_id; //创建应用后,分配给应用的appKey
  26. public static String client_secret = StateData.client_secret; //创建应用后,分配给应用的appSecret
  27. public static String token_grant_type = StateData.grant_type_token; //授权类型,在本步骤中,此值为“authorization_code”
  28. public static String redirect_uri = StateData.redirect_uri; //回调地址,必传且不能为空
  29. public static void main(String[] args) {
  30. StateData.taxNum = "914403003120360455";
  31. TestOpenApiV1.setHeader();
  32. //开发者通过code可以换取access_token、授权商户的uerId
  33. String code = StateData.auth_code;//临时授权码code,isv用户通过页面请求(authorize.html)返回code值;
  34. String taxNum = StateData.taxNum;//授权企业的税号,isv用户通过页面请求(authorize.html)返回taxnum值;
  35. String resValue = getAccessToken();
  36. /**
  37. * ISV用户定时刷新token
  38. * refresh_token提供给开发者一种延长access_token有效期的机制,提升开发者提供给商户应用的使用流畅性。
  39. * 开发者使用code获取access_token时会返回一个refresh_token,在refresh_token有效期内可以通过refresh_token刷新access_token有效时长。
  40. * 注:refresh_token超时时,需重走授权流程
  41. */
  42. // if (!StringUtils.isEmpty(resValue)) {
  43. // Map<String, Object> map = parseJSON2Map(resValue);
  44. // new RefreshTokenTimer( _url, _header,
  45. // String.valueOf(map.get("refresh_token")), client_secret, String.valueOf(map.get("userId"))).executeTimer();
  46. // } else {
  47. // System.out.println("获取授权码报错");
  48. // }
  49. }
  50. /**
  51. * 获取去token
  52. * @return
  53. */
  54. public static String getAccessToken() {
  55. CloseableHttpClient client = null;
  56. CloseableHttpResponse response = null;
  57. try {
  58. client = HttpClients.createDefault();
  59. HttpUriRequest build = RequestBuilder
  60. .post(_url)
  61. .addHeader("Content-Type",_header)
  62. .addParameter("client_id", client_id)
  63. .addParameter("client_secret", client_secret)
  64. .addParameter("grant_type", token_grant_type)
  65. .build();
  66. //请求
  67. response = client.execute(build);
  68. HttpEntity entity = response.getEntity();
  69. String resValue = "";
  70. if (entity != null) {
  71. resValue = EntityUtils.toString(entity);
  72. System.out.println("response: " + resValue);
  73. }
  74. //释放资源
  75. EntityUtils.consume(entity);
  76. return resValue;
  77. } catch (Exception e) {
  78. e.printStackTrace();
  79. }finally{
  80. try {
  81. client.close();
  82. response.close();
  83. } catch (IOException e) {
  84. // TODO 自动生成的 catch 块
  85. e.printStackTrace();
  86. }
  87. }
  88. return "更新token失败";
  89. }
  90. /**
  91. * 获取去token
  92. * @return
  93. */
  94. public static String getAccessToken(String code, String taxNum) {
  95. CloseableHttpClient client = null;
  96. CloseableHttpResponse response = null;
  97. try {
  98. client = HttpClients.createDefault();
  99. HttpUriRequest build = RequestBuilder
  100. .post(_url)
  101. .addHeader("Content-Type",_header)
  102. .addParameter("client_id", client_id)
  103. .addParameter("client_secret", client_secret)
  104. .addParameter("grant_type", token_grant_type)
  105. .addParameter("redirect_uri", redirect_uri)
  106. .addParameter("code", code)
  107. .addParameter("taxNum", taxNum)
  108. .build();
  109. //请求
  110. response = client.execute(build);
  111. HttpEntity entity = response.getEntity();
  112. String resValue = "";
  113. if (entity != null) {
  114. resValue = EntityUtils.toString(entity);
  115. System.out.println("response: " + resValue);
  116. }
  117. //释放资源
  118. EntityUtils.consume(entity);
  119. return resValue;
  120. } catch (Exception e) {
  121. e.printStackTrace();
  122. }finally{
  123. try {
  124. client.close();
  125. response.close();
  126. } catch (IOException e) {
  127. // TODO 自动生成的 catch 块
  128. e.printStackTrace();
  129. }
  130. }
  131. return null;
  132. }
  133. /**
  134. * json转map
  135. * @param jsonStr
  136. * @return
  137. */
  138. public static Map<String, Object> parseJSON2Map(String jsonStr) {
  139. Map<String, Object> map = new HashMap<String, Object>();
  140. //最外层解析
  141. JSONObject json = JSONObject.fromObject(jsonStr);
  142. for (Object k : json.keySet()) {
  143. Object v = json.get(k);
  144. //如果内层还是数组的话,继续解析
  145. if (v instanceof JSONArray) {
  146. List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
  147. Iterator<JSONObject> it = ((JSONArray) v).iterator();
  148. while (it.hasNext()) {
  149. JSONObject json2 = it.next();
  150. list.add(parseJSON2Map(json2.toString()));
  151. }
  152. map.put(k.toString(), list);
  153. } else {
  154. map.put(k.toString(), v);
  155. }
  156. }
  157. return map;
  158. }
  159. }