File was renamed from src/main/java/com/zy/nc/Test.java |
| | |
| | | * @author lizhmf |
| | | * @date 2019年6月20日上午10:53:11 |
| | | */ |
| | | public class Test { |
| | | public class SendUtil { |
| | | |
| | | // app_secret |
| | | private static String client_secret = null; |
| | |
| | | } |
| | | System.out.println("getTokenData:" + token); |
| | | // 测试openapi |
| | | testApi(token); |
| | | //sendDataToNc(token); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 请求openapi |
| | | * |
| | | * @param token |
| | | * @throws Exception |
| | | */ |
| | | public static NcResultMessage sendDataToNc(String token, String baseUrl, String apiUrl, String requestBody) { |
| | | if (token == null) { |
| | | try { |
| | | token = getToken(); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | // token转对象,获取api访问所用token和secret |
| | | NcResultMessage returnData = JSONObject.parseObject(token, NcResultMessage.class); |
| | | if (returnData.isSuccess()) { |
| | | JSONObject data = JSONObject.parseObject(returnData.getData().toString()); |
| | | String access_token = (String) data.get("access_token"); |
| | | String security_key = (String) data.get("security_key"); |
| | | String refresh_token = (String) data.get("refresh_token"); |
| | | Long expire_in = data.getLong("expires_in"); |
| | | Long ts = data.getLong("ts"); |
| | | if (ts + expire_in < System.currentTimeMillis()) { |
| | | try { |
| | | token = getToken(); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | data = JSONObject.parseObject(token.getBytes(), NcResultMessage.class); |
| | | access_token = (String) data.get("access_token"); |
| | | security_key = (String) data.get("security_key"); |
| | | refresh_token = (String) data.get("refresh_token"); |
| | | } |
| | | System.out.println("【ACCESS_TOKEN】:" + access_token); |
| | | // 请求路径 |
| | | String url = baseUrl + apiUrl; |
| | | // header 参数 |
| | | Map<String, String> headermap = new HashMap<>(); |
| | | headermap.put("access_token", access_token); |
| | | headermap.put("client_id", client_id); |
| | | StringBuffer sb = new StringBuffer(); |
| | | sb.append(client_id); |
| | | if (!Cools.isEmpty(requestBody)) { |
| | | // sb.append(requestBody.replaceAll("\\s*|\t|\r|\n", "").trim()); |
| | | sb.append(requestBody); |
| | | } |
| | | sb.append(pubKey); |
| | | String sign = SHA256Util.getSHA256(sb.toString(), pubKey); |
| | | headermap.put("signature", sign); |
| | | if (!Cools.isEmpty(busi_id)) { |
| | | headermap.put("busi_id", busi_id); |
| | | } |
| | | if (!Cools.isEmpty(repeat_check)) { |
| | | headermap.put("repeat_check", repeat_check); |
| | | } |
| | | // headermap.put("ucg_flag", "y"); |
| | | String mediaType = "application/json;charset=utf-8"; |
| | | // 表体数据json |
| | | // 根据安全级别选择加密或压缩请求表体参数 |
| | | String json = null; |
| | | String result; |
| | | String result2; |
| | | try { |
| | | json = dealRequestBody(requestBody, security_key, secret_level); |
| | | // 返回值 |
| | | result = doPost(url, null, mediaType, headermap, json); |
| | | result2 = dealResponseBody(result, security_key, secret_level); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | System.out.println("【RESULT】:" + result); |
| | | System.out.println("result解密:" + result2); |
| | | return JSONObject.parseObject(result2, NcResultMessage.class); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | |
| | | return token; |
| | | } |
| | | |
| | | /** |
| | | * 请求openapi |
| | | * |
| | | * @param token |
| | | * @throws Exception |
| | | */ |
| | | private static void testApi(String token) throws Exception { |
| | | // token转对象,获取api访问所用token和secret |
| | | ResultMessageUtil returnData = JSONObject.parseObject(token, ResultMessageUtil.class); |
| | | Map<String, Object> data = (Map<String, Object>) returnData.getData(); |
| | | String access_token = (String) data.get("access_token"); |
| | | String security_key = (String) data.get("security_key"); |
| | | String refresh_token = (String) data.get("refresh_token"); |
| | | long expire_in = new Double((double) data.get("expires_in")).longValue(); |
| | | long ts = new Double((double) data.get("ts")).longValue(); |
| | | if (ts + expire_in < System.currentTimeMillis()) { |
| | | token = getToken(); |
| | | returnData = JSONObject.parseObject(token, ResultMessageUtil.class); |
| | | data = (Map<String, Object>) returnData.getData(); |
| | | access_token = (String) data.get("access_token"); |
| | | security_key = (String) data.get("security_key"); |
| | | refresh_token = (String) data.get("refresh_token"); |
| | | } |
| | | System.out.println("【ACCESS_TOKEN】:" + access_token); |
| | | |
| | | // 请求路径 |
| | | String url = baseUrl + apiUrl; |
| | | // header 参数 |
| | | Map<String, String> headermap = new HashMap<>(); |
| | | headermap.put("access_token", access_token); |
| | | headermap.put("client_id", client_id); |
| | | |
| | | StringBuffer sb = new StringBuffer(); |
| | | sb.append(client_id); |
| | | if (!Cools.isEmpty(requestBody)) { |
| | | // sb.append(requestBody.replaceAll("\\s*|\t|\r|\n", "").trim()); |
| | | sb.append(requestBody); |
| | | } |
| | | sb.append(pubKey); |
| | | String sign = SHA256Util.getSHA256(sb.toString(), pubKey); |
| | | headermap.put("signature", sign); |
| | | |
| | | if (!Cools.isEmpty(busi_id)) { |
| | | headermap.put("busi_id", busi_id); |
| | | } |
| | | if (!Cools.isEmpty(repeat_check)) { |
| | | headermap.put("repeat_check", repeat_check); |
| | | } |
| | | // headermap.put("ucg_flag", "y"); |
| | | |
| | | String mediaType = "application/json;charset=utf-8"; |
| | | |
| | | // 表体数据json |
| | | // 根据安全级别选择加密或压缩请求表体参数 |
| | | String json = dealRequestBody(requestBody, security_key, secret_level); |
| | | |
| | | // 返回值 |
| | | String result = doPost(url, null, mediaType, headermap, json); |
| | | String result2 = dealResponseBody(result, security_key, secret_level); |
| | | System.out.println("【RESULT】:" + result); |
| | | System.out.println("result解密:" + result2); |
| | | } |
| | | |
| | | /** |
| | | * 返回值进行过加密和压缩,对返回值进行解压和解密 |