请求示例代码-JAVA版-简易版
<pre><code class="language-java">package com.yzm.center.open.gateway;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.Serializable;
@Slf4j
public class aaa {
private static String url = &quot;https://open-beta.56yzm.com:8443&quot;;
private static String clientId = &quot;分配的clientId&quot;;
private static String clientSecret = &quot;管理员分配的clientSecret&quot;;
private static String sign_secret_key = &quot;管理员分配的sign_secret_key&quot;;
private static String ivs_public_key = &quot;管理员分配的ivs_public_key&quot;;
private static String userCode = &quot;管理员分配的user-code&quot;;
public static void main(String[] args) {
String apiUrl = String.format(&quot;%s%s&quot;, url, &quot;/api-server/openapi/vehicle/v2/createVehicle&quot;);
String reqJsonStr = &quot;{\&quot;carInfoDto\&quot;:{\&quot;carDrivingCopyAddImg\&quot;:\&quot;https://file.rlsk.link/12961571/fb14799fda39444d978972d9b85204f3.jpeg\&quot;,\&quot;carDrivingCopyImg\&quot;:\&quot;https://file.rlsk.link/12961571/fb14799fda39444d978972d9b85204f3.jpeg\&quot;,\&quot;carDrivingFrontImg\&quot;:\&quot;https://file.rlsk.link/12961571/fb14799fda39444d978972d9b85204f3.jpeg\&quot;,\&quot;carLength\&quot;:11,\&quot;carTypesOf\&quot;:17,\&quot;plateColor\&quot;:\&quot;YELLOW\&quot;,\&quot;plateNo\&quot;:\&quot;晋K62255\&quot;,\&quot;roadTransportPermImg\&quot;:\&quot;https://file.rlsk.link/12961571/fb14799fda39444d978972d9b85204f3.jpeg\&quot;,\&quot;trailerCarDrivingCopyImg\&quot;:\&quot;https://file.rlsk.link/12961571/fb14799fda39444d978972d9b85204f3.jpeg\&quot;,\&quot;trailerCarDrivingFrontImg\&quot;:\&quot;https://file.rlsk.link/12961571/fb14799fda39444d978972d9b85204f3.jpeg\&quot;,\&quot;trailerNo\&quot;:\&quot;晋K6U92挂\&quot;,\&quot;useCharacter\&quot;:\&quot;货运\&quot;},\&quot;driverInfoDto\&quot;:{\&quot;driverName\&quot;:\&quot;孟庆祯\&quot;,\&quot;drivingCopyImg\&quot;:\&quot;https://file.rlsk.link/12961571/fb14799fda39444d978972d9b85204f3.jpeg\&quot;,\&quot;drivingImg\&quot;:\&quot;https://file.rlsk.link/12961571/fb14799fda39444d978972d9b85204f3.jpeg\&quot;,\&quot;idCardBackImg\&quot;:\&quot;https://file.rlsk.link/12961571/fb14799fda39444d978972d9b85204f3.jpeg\&quot;,\&quot;idCardFrontImg\&quot;:\&quot;https://file.rlsk.link/12961571/fb14799fda39444d978972d9b85204f3.jpeg\&quot;,\&quot;idCardNo\&quot;:\&quot;429006199005131414\&quot;,\&quot;phone\&quot;:\&quot;15113151415\&quot;,\&quot;qualificationCardImg\&quot;:\&quot;https://file.rlsk.link/12961571/fb14799fda39444d978972d9b85204f3.jpeg\&quot;},\&quot;yzmUserCode\&quot;:\&quot;YZM_9dO0fhTtOt\&quot;}&quot;;
String token = getYzmToken(clientId, clientSecret);
YzmSignDto yzmSign = getSign(reqJsonStr);
YzmResultBasicDto yzmResultBasicDto = postToYzm(YzmResultBasicDto.class, apiUrl, token, yzmSign, userCode);
System.out.println(JSONObject.toJSONString(yzmResultBasicDto));
}
/**
* 获取 token
*
* @param clientId
* @param clientSecret
* @return
*/
public static String getYzmToken(String clientId, String clientSecret) {
HttpResponse httpResponse = HttpUtil
.createPost(String.format(&quot;%s%s&quot;, url, &quot;/open-oauth/oauth/token&quot;))
.contentType(&quot;application/x-www-form-urlencoded&quot;)
.form(&quot;grant_type&quot;, &quot;client_credentials&quot;)
.form(&quot;client_id&quot;, clientId)
.form(&quot;client_secret&quot;, clientSecret)
.form(&quot;scope&quot;, &quot;read&quot;)
.execute();
if (httpResponse.getStatus() != 200) {
log.error(&quot;运之盟-开放平台登录异常:{}&quot;, httpResponse.getStatus());
}
String body = httpResponse.body();
YzmTokenDto yzmTokenDto = JSONObject.parseObject(body, YzmTokenDto.class);
return yzmTokenDto.getAccessToken();
}
/**
* 生成签名
*
* @param jsonStr 待签名参数 json
* @return
*/
private static YzmSignDto getSign(String jsonStr) {
try {
HttpResponse authorization = HttpUtil.createPost(url + &quot;/crypto/sign&quot;)
.contentType(&quot;application/json&quot;)
.header(&quot;X-SignKey&quot;, sign_secret_key)
.header(&quot;X-CryptoKey&quot;, ivs_public_key)
.body(jsonStr)
.execute();
//验证结果
if (authorization.getStatus() != 200) {
log.error(&quot;请求异常&quot;);
}
JSONObject jsonObjectSign = JSONObject.parseObject(authorization.body());
YzmSignDto yzmSignDto = new YzmSignDto();
yzmSignDto.setSign(jsonObjectSign.getString(&quot;X-Signature&quot;));
yzmSignDto.setNonce(jsonObjectSign.getLong(&quot;X-Nonce&quot;));
yzmSignDto.setTimeMillis(jsonObjectSign.getLong(&quot;X-Timestamp&quot;));
yzmSignDto.setBodyJson(jsonObjectSign.getJSONObject(&quot;requestBody&quot;));
return yzmSignDto;
} catch (Exception e) {
log.error(&quot;获取签名异常:&quot; + e);
return null;
}
}
/**
* @param clazz 待转换类型
* @param apiUrl 请求地址
* @param token token
* @param yzmSign 当前毫秒时间戳
* @param &lt;T&gt; 返回类型
* @return
*/
private static &lt;T&gt; T postToYzm(Class&lt;T&gt; clazz, String apiUrl, String token, YzmSignDto yzmSign, String userCode) {
long startTime = System.currentTimeMillis();
log.info(&quot;运之盟开放平台-请求参数:{}&quot;, yzmSign.getBodyJson().toJSONString());
HttpResponse authorization = HttpUtil.createPost(apiUrl)
.contentType(&quot;application/json&quot;)
.header(&quot;Authorization&quot;, &quot;Bearer &quot; + token)
.header(&quot;X-Timestamp&quot;, String.valueOf(yzmSign.getTimeMillis()))
.header(&quot;X-Nonce&quot;, String.valueOf(yzmSign.getNonce()))
.header(&quot;X-Signature&quot;, yzmSign.getSign())
.header(&quot;user-code&quot;, userCode)
.body(yzmSign.getBodyJson().toJSONString())
.execute();
//验证结果
if (authorization.getStatus() != 200) {
log.error(&quot;请求异常&quot;);
}
long endTime = System.currentTimeMillis();
log.info(&quot;运之盟开放平台-耗时:{},返回参数:{}&quot;, endTime - startTime, authorization.body());
return JSONObject.parseObject(authorization.body(), clazz);
}
@Data
public static class YzmSignDto {
public JSONObject bodyJson;
public String sign;
public long timeMillis;
public long nonce;
}
@Data
public static class YzmResultBasicDto&lt;T&gt; implements Serializable {
public String code;
public String info;
public T data;
}
@Data
public static class YzmTokenDto {
public String accessToken;
public String tokenType;
public Long expiresIn;
public String scope;
}
}
</code></pre>