Browse Source
Conflicts: epmet-module/data-aggregator/data-aggregator-server/src/main/java/com/epmet/dataaggre/service/govorg/impl/GovOrgServiceImpl.javadev
7 changed files with 147 additions and 5 deletions
@ -0,0 +1,137 @@ |
|||
package com.epmet.test.openapi; |
|||
|
|||
import com.alibaba.fastjson.JSON; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.epmet.openapi.sdk.sign.OpenApiSignUtils; |
|||
import com.google.gson.JsonObject; |
|||
import org.apache.http.client.methods.CloseableHttpResponse; |
|||
import org.apache.http.client.methods.HttpPost; |
|||
import org.apache.http.entity.StringEntity; |
|||
import org.apache.http.impl.client.CloseableHttpClient; |
|||
import org.apache.http.impl.client.HttpClientBuilder; |
|||
import org.apache.http.util.EntityUtils; |
|||
import org.junit.Test; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.UUID; |
|||
|
|||
/** |
|||
* TakeToken方式的第三方平台接入测试类 |
|||
*/ |
|||
public class TestTakeTokenOpenApi { |
|||
|
|||
// 这是测试环境的
|
|||
//体悟实训的appId和secret
|
|||
String appId = "61161d2f4b9c045859b7de5df93dd8b2"; |
|||
String authType = "take_token"; |
|||
String secret = "4d8e8fc9bacb41bba94e715f5fe2e0a36ae9c4150ade4436ad2c9448c486cc9c"; |
|||
|
|||
@Test |
|||
public void testIt() throws Exception { |
|||
String accessToken = getAccessToken(); |
|||
execBusinessWithSign(accessToken); |
|||
//execBusinessWithoutSign(accessToken);
|
|||
|
|||
} |
|||
|
|||
/** |
|||
* 获取accessToken |
|||
* @return |
|||
* @throws Exception |
|||
*/ |
|||
private String getAccessToken() throws Exception { |
|||
String timestamp = String.valueOf(System.currentTimeMillis()); |
|||
String nonce = UUID.randomUUID().toString().replace("-", ""); |
|||
|
|||
HashMap<String, String> content = new HashMap<>(); |
|||
content.put("app_id", appId); |
|||
content.put("timestamp", timestamp); |
|||
content.put("nonce", nonce); |
|||
content.put("auth_type", authType); |
|||
|
|||
String sign = OpenApiSignUtils.createSign(content, secret); |
|||
|
|||
String takeTokenUrl = String.format("https://epmet-dev.elinkservice.cn/api/epmet/ext/open-api/get-access-token?auth_type=%s&app_id=%s×tamp=%s&sign=%s&nonce=%s", authType, appId, timestamp, sign, nonce); |
|||
|
|||
CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
|||
HttpPost httpPost = new HttpPost(takeTokenUrl); |
|||
CloseableHttpResponse response = httpClient.execute(httpPost); |
|||
String result = EntityUtils.toString(response.getEntity()); |
|||
JSONObject resultObject = JSON.parseObject(result).getJSONObject("data"); |
|||
|
|||
return resultObject.getString("accessToken"); |
|||
} |
|||
|
|||
/** |
|||
* 执行需要校验签名业务方法 |
|||
* @param accessToken |
|||
*/ |
|||
private void execBusinessWithSign(String accessToken) throws Exception { |
|||
String orgId = "1"; |
|||
String test = "2"; |
|||
|
|||
String timestamp = String.valueOf(System.currentTimeMillis()); |
|||
String nonce = UUID.randomUUID().toString().replace("-", ""); |
|||
|
|||
// 签名参数
|
|||
HashMap<String, String> createSignParams = new HashMap<>(); |
|||
createSignParams.put("orgId", orgId); |
|||
createSignParams.put("test", test); |
|||
|
|||
createSignParams.put("app_id", appId); |
|||
createSignParams.put("timestamp", timestamp); |
|||
createSignParams.put("nonce", nonce); |
|||
createSignParams.put("auth_type", authType); |
|||
|
|||
String sign = OpenApiSignUtils.createSign(createSignParams, secret); |
|||
|
|||
String businessUrl = String.format("https://epmet-dev.elinkservice.cn/api/epmet/ext/open-api/get-org-detail?auth_type=%s&app_id=%s×tamp=%s&sign=%s&nonce=%s", authType, appId, timestamp, sign, nonce); |
|||
|
|||
// 业务参数
|
|||
JsonObject bizParam = new JsonObject(); |
|||
bizParam.addProperty("orgId", orgId); |
|||
bizParam.addProperty("test", test); |
|||
|
|||
// 发送请求
|
|||
CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
|||
HttpPost httpPost = new HttpPost(businessUrl); |
|||
httpPost.addHeader("accesstoken", accessToken); |
|||
httpPost.addHeader("content-type", "application/json;charset=utf-8"); |
|||
httpPost.setEntity(new StringEntity(bizParam.toString(), "utf-8")); |
|||
CloseableHttpResponse response = httpClient.execute(httpPost); |
|||
|
|||
// 解析结果
|
|||
String result = EntityUtils.toString(response.getEntity()); |
|||
System.out.println(result); |
|||
} |
|||
|
|||
/** |
|||
* 执行不校验签名业务方法 |
|||
* @param accessToken |
|||
*/ |
|||
private void execBusinessWithoutSign(String accessToken) throws Exception { |
|||
String orgId = "1"; |
|||
String test = "2"; |
|||
|
|||
// 不需要签名,但是仍然要传递app_id和auth_type参数,以及accesstoken
|
|||
String businessUrl = String.format("http://localhost:8080/api/epmet/ext/open-api/get-org-detail?auth_type=%s&app_id=%s", authType, appId); |
|||
|
|||
// 业务参数
|
|||
JsonObject bizParam = new JsonObject(); |
|||
bizParam.addProperty("orgId", "3"); |
|||
bizParam.addProperty("test", test); |
|||
|
|||
// 发送请求
|
|||
CloseableHttpClient httpClient = HttpClientBuilder.create().build(); |
|||
HttpPost httpPost = new HttpPost(businessUrl); |
|||
httpPost.addHeader("accesstoken", accessToken); |
|||
httpPost.addHeader("content-type", "application/json;charset=utf-8"); |
|||
httpPost.setEntity(new StringEntity(bizParam.toString(), "utf-8")); |
|||
CloseableHttpResponse response = httpClient.execute(httpPost); |
|||
|
|||
// 解析结果
|
|||
String result = EntityUtils.toString(response.getEntity()); |
|||
System.out.println(result); |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue