对接第三⽅阿⾥云api接⼝⽰例代码
最近对接了⼀个第三⽅阿⾥云api的接⼝,过程并不是很顺利,所以把最后成功的代码作为案例发出来,为了⽅便对后⾯有需要的⼈,有⼀些参考把。代码没做太多封装
public List<ALaDingPrice> getPrice() throws Exception {
String sufUrl = "请求地址";
List<String> arrayPriceStrings = getALaDDinData(sufUrl);
List<ALaDingPrice> result = arrayPriceStrings.stream().map(obj->JSON.parseObject(obj, ALaDingPrice.class)).List());
return result;
}
public List<ALaDingStock> getStock() throws Exception {
String sufUrl = "请求地址";
List<String> arrayPriceStrings = getALaDDinData(sufUrl);
List<ALaDingStock> result = arrayPriceStrings.stream().map(obj->JSON.parseObject(obj, ALaDingStock.class)).List());
return result;
}
private List<String> getALaDDinData(String sufUrl) throws Exception {
Map body = new HashMap<String, List<String>>() {
{
put("sku", wArrayList());
}
};
String bodyString = JSONString(body);
String md5 = calcMd5(bodyString);
String nonce = UUID.randomUUID().toString();
String signatureMethod = "HmacSHA256";百度api接口
String signatureHeaders = "x-ca-key,x-ca-nonce,x-ca-signaturemethod";
String contentType = "application/json";
String accept = getDefaultAccept(restTemplate, String.class);
String date = String.valueOf(System.currentTimeMillis());
String stringToSign = "POST\n" +
"{accept}\n" +
"{md5}\n" +
"{contentType}\n" +
"{date}\n" +
"x-ca-key:{appKey}\n" +
"x-ca-nonce:{nonce}\n" +
"x-ca-signaturemethod:{signatureMethod}\n" +
"{url}";
stringToSign = placeAll("[{]accept[}]",accept)
.replaceAll("[{]md5[}]",md5)
.replaceAll("[{]contentType[}]",contentType)
.replaceAll("[{]date[}]",date)
.replaceAll("[{]appKey[}]",appKey)
.replaceAll("[{]nonce[}]",nonce)
.
replaceAll("[{]signatureMethod[}]",signatureMethod)
.replaceAll("[{]url[}]",sufUrl);
String signature = sign(stringToSign, secret);
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization","Bearer "+authorization);
headers.add("Date",date);
headers.add("Content-MD5",md5);
headers.add("X-Ca-Nonce",nonce);
headers.add("X-Ca-Key",appKey);
headers.add("X-Ca-Signature",signature);
headers.add("X-Ca-SignatureMethod",signatureMethod);
headers.add("X-Ca-Signature-Headers",signatureHeaders);
headers.add("Content-Type",contentType);
ResponseEntity<String> restStrResult = hange(sufUrl, HttpMethod.POST,
new HttpEntity<Object>(body, headers),String.class);
List<String> arrayPriceStrings = (List<String>) ((Map)JSON.Body(),
LinkedHashMap.class).get("body")).get("response_list");
return arrayPriceStrings;
}
//获取签名
private String sign(String stringToSign, String secret) throws Exception {
Mac sha256_HMAC = Instance("HmacSHA256");
SecretKeySpec secret_key = new Bytes(), "HmacSHA256");
sha256_HMAC.init(secret_key);
deBase64String(sha256_HMAC.Bytes()));
}
//md5加密
private String calcMd5(String data) throws NoSuchAlgorithmException {
MessageDigest md = Instance("md5");
byte[] md5 = md.Bytes());
BASE64Encoder be = new BASE64Encoder();
String base64 = be.encode(md5);
return base64;
}
//获取请求头部Accept内容
private String getDefaultAccept(RestTemplate restTemplate, Class responseType) {
if (responseType != null) {
Class<?> responseClass = null;
if (responseType instanceof Class) {
responseClass = responseType;
}
List<MediaType> allSupportedMediaTypes = new ArrayList();
Iterator var4 = MessageConverters().iterator();
while(var4.hasNext()) {
HttpMessageConverter<?> converter = (();
if (responseClass != null) {
if (converter.canRead(responseClass, (MediaType)null)) {
allSupportedMediaTypes.SupportedMediaTypes(converter));
}
} else if (converter instanceof GenericHttpMessageConverter) {
GenericHttpMessageConverter<?> genericConverter = (GenericHttpMessageConverter)converter; if (genericConverter.canRead(responseType, (Class)null, (MediaType)null)) {
allSupportedMediaTypes.SupportedMediaTypes(converter));
}
}
}
if (!allSupportedMediaTypes.isEmpty()) {
MediaType.sortBySpecificity(allSupportedMediaTypes);
}
if (!allSupportedMediaTypes.isEmpty()) {
MediaType.sortBySpecificity(allSupportedMediaTypes);
String(allSupportedMediaTypes);
}
}
return "";
}
private List<MediaType> getSupportedMediaTypes (HttpMessageConverter < ? > messageConverter){
List<MediaType> supportedMediaTypes = SupportedMediaTypes();
List<MediaType> result = new ArrayList(supportedMediaTypes.size());
MediaType supportedMediaType;
for(Iterator var4 = supportedMediaTypes.iterator(); var4.hasNext(); result.add(supportedMediaType)) {
supportedMediaType = (();
if (Charset() != null) {
supportedMediaType = new Type(), Subtype()); }
}
return result;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论