原创

阿奇索平台接口对接记录:授权、请求与结果处理

保留第三方平台对接中的需求背景和调用示例,梳理授权、参数映射与结果处理,并说明接口变更和失败重试需要核对的内容。

第三方对接应将业务状态与接口返回状态分开处理。一次请求超时可能是未执行,也可能是已经执行但响应丢失,因此重试前应先了解接口的幂等规则。历史参数与地址仅代表当时记录,接入时应依据账户实际可用的官方接口文档。

一 客户需求

在系统录单时自动获取淘宝订单价格,在系统操作订单为已完成时,该订单在淘宝自动发货。

二 第三方支持(阿奇索)

文档地址https://open.agiso.com/document/#/alds/guide

1.准备工作

在阿齐索开放平台申请自己的账号,根据平台要求认证一些相关资料,创建自己的 Appid (自动生成对应的secret),让客户登录该淘宝授权地址https://alds.agiso.com/Open/Authorize.aspx 。授权完成之后将获取到的AccessToken拿到。

2. 开发流程

1.一个淘宝商铺对应一个AccessToken,客户不止一个淘宝店铺

2.需要使用到的功能,获取淘宝订单详细信息,自动推送旺旺消息,自动发货功能等等

3. 实现

将平台的Appid ,secret,淘宝授权的AccessToken 在数据库建立对应的字段,将这些数据保存到数据库,这样不同的淘宝店铺调用阿齐索的接口时,获取到对应的淘宝店铺授权的AccessToken就可以了。

阿齐索调用接口的工具类,满足一个工具类调用不同的阿齐索接口。

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.cdchengqitech.business.pojo.TaobaoInterfaceBPo;

public class AQiSuoUtil {
	/*inParm是调用方法时传入的参数,tid是淘宝的订单号,method是阿齐索对应的接口url
         TaobaoInterfaceBPo 实体类是存放有平台的appid和secret,淘宝的授权码accessToken。
         这样修改可以满足一个方法调用阿齐索很多的接口
         */
	public static String temp(String inParam,String tid,String method,TaobaoInterfaceBPo tb){
		String appSecret = tb.getAppSecret();//平台的appSecret
	    String accessToken = tb.getAccessToken();//淘宝授权码
	    CloseableHttpClient httpclient = HttpClients.createDefault();

	    HttpPost httpPost = new HttpPost(method);//调用的接口

	    //设置头部
	    httpPost.addHeader("Authorization","Bearer "+ accessToken);
	    httpPost.addHeader("ApiVersion","1");

	    //业务参数
	    Map<String, String> data = new HashMap<String, String>();
	    data.put(inParam, tid);//调用接口对应的参数,有个tips分享,inParam在tid参数为
            单个订单号的时候,这个值为tid,如果tid参数是多参,inParam为tids
//	    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    Long timestamp = System.currentTimeMillis() / 1000;
	    data.put("timestamp", timestamp.toString());
	    //参数签名
	    try {
	        data.put("sign", Sign(data,appSecret));
	    } catch (NoSuchAlgorithmException e) {
	        e.printStackTrace();
	    } catch (UnsupportedEncodingException e) {
	        e.printStackTrace();
	    }
	    List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
	    for (Map.Entry<String, String> entry : data.entrySet()) {
	        params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
	    }
	    //发起POST请求
	    try {
	        httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
	        HttpResponse httpResponse = httpclient.execute(httpPost);
	        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
	            return EntityUtils.toString(httpResponse.getEntity());
	        } else {
	            return ("doPost Error Response: " + httpResponse.getStatusLine().toString());
	        }
	    } catch (Exception e) {
	        e.printStackTrace();
	        return null;
	    }
	}
	//参数签名
	public static String Sign(Map<String, String> params,String appSecret) throws NoSuchAlgorithmException, UnsupportedEncodingException
	{
	    String[] keys = params.keySet().toArray(new String[0]);
	    Arrays.sort(keys);

	    StringBuilder query = new StringBuilder();
	    query.append(appSecret);
	    for (String key : keys) {
	        String value = params.get(key);
	        query.append(key).append(value);
	    }
	    query.append(appSecret);

	    byte[] md5byte = encryptMD5(query.toString());

	    return  byte2hex(md5byte);
	}
	//byte数组转成16进制字符串
	public static String byte2hex(byte[] bytes) {
	    StringBuilder sign = new StringBuilder();
	    for (int i = 0; i < bytes.length; i++) {
	        String hex = Integer.toHexString(bytes[i] & 0xFF);
	        if (hex.length() == 1) {
	            sign.append("0");
	        }
	        sign.append(hex.toLowerCase());
	    }
	    return sign.toString();
	}
	//Md5摘要
	public static byte[] encryptMD5(String data) throws NoSuchAlgorithmException, UnsupportedEncodingException {
	    MessageDigest md5 = MessageDigest.getInstance("MD5");
	    return md5.digest(data.getBytes("UTF-8"));
	}


}

调用该方法的返回值在开放平台有介绍,写了一个类对应该返回值参数,然后使用json转化为对象

import com.fasterxml.jackson.annotation.JsonInclude;

import lombok.Data;
import lombok.experimental.Accessors;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@Accessors(chain = true)
public class TaoBaoResp {
	private Boolean IsSuccess;
	private TaoBaoPo Data;

	public static Response success() {
		return new Response();
	}

	public static Response success(TaoBaoPo Data) {
		return new Response().setData(Data);
	}

	public static Response fail(String msg) {
		return new Response().setCode(-1).setMsg(msg);
	}
}

@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class TaoBaoPo {

        //参数有很多,我只取了自己需要的一部分
	private String BuyerNick;

	private BigDecimal Price;

	private BigDecimal TotalFee;

	private String ResultMsg;

	private Integer ResultCode;

	private String ErrorMsg;

	private Integer ErrorCode;
}

最后就可以根据实际需求来调用阿齐索接口获取订单价格和完成自动发货功能了。

正文到此结束