forked from rongchao/epmet-cloud-rizhao
4 changed files with 142 additions and 2 deletions
@ -0,0 +1,135 @@ |
|||||
|
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 { |
||||
|
|
||||
|
String appId = "1504335474091569153"; |
||||
|
String authType = "take_token"; |
||||
|
String secret = "70e7ee0592d94affaa6e7b463926a3dd3cf1606945644baf810f93e8e9638c50"; |
||||
|
|
||||
|
@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("http://localhost:8080/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("http://localhost:8080/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", "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); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 执行不校验签名业务方法 |
||||
|
* @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