APIs in Aliexpress Open Platform need to be invoked based on the Hypertext Transfer Protocol (HTTP). To invoke a specific API: The developer (such as the ISV) has two choices:
Basically, the developers could choose developing their own HTTP invocation and interpretation module or using TOP SDK to interact with TOP server. The following sections describes the procedures of API invocation based on the second option. However, even if using SDK directly, it is still recommended for developers to read the following sections.
The API invocation process mainly has the following steps.
- Fill in parameters.
- Generate a signature.
- Encapsulate an HTTP request.
- Initiate the HTTP request
- Receive an HTTP response.
- Interpret json/xml results
Based on the service URL used to invoke APIs, the AliExpress open platform currently provides two environments for ISVs: Chinese environment, and overseas environment. The seller could initiate a performance test before deciding which url to choose.
Invocation Environment | Service URL (HTTP) | Service URL (HTTPS) |
---|---|---|
Chinese environment | http://gw.api.taobao.com/router/rest | https://eco.taobao.com/router/rest |
Overseas environment | http://api.taobao.com/router/rest | https://api.taobao.com/router/rest |
Note: The default service url in the SDK is http://gw.api.taobao.com/router/rest
Public parameters need to be set for every API. The following table lists all public parameters.
Parameter Name | Parameter Type | Mandatory or Optional | Parameter Description |
---|---|---|---|
method | String | Mandatory | Indicates the API name. |
app_key | String | Mandatory | Indicates the AppKey allocated by the TOP to an application. An ISV can choose Open Platform Console > Application Management > Overview to check the AppKey and AppSecret of the formal environment. |
session | String | Mandatory | Indicates the authorization granted by the TOP to an application after a user logs in and grants authorization successfully. For details, click here |
timestamp | String | Mandatory | Indicates the time stamp in the format of yyyy-MM-dd HH:mm:ss and in the time zone of GMT+8. For example, 2016-01-01 12:00:00. The Taobao API server allows a maximum time error of 10 minutes for a request from a client. |
format | String | Optional | Indicates the response format. The default value is xml. The value can be set to xml or json. |
v | String | Mandatory | Indicates the API protocol version. The value can be set to 2.0. |
simplify | Boolean | Optional | Indicates whether the simplified JSON return format is used. This parameter is valid only if format is set to json. The default value is false. |
sign_method | String | Mandatory | Indicates the signature digest algorithm. The value can be set to hmac or md5. |
sign | String | Mandatory | Indicates the obtained signature of API input parameters. For details about the signature algorithm, see the following description. |
Besides public parameters, for each specific API, there are some input parameters.
To prevent data from being tampered maliciously by a hacker during API invocation, each request for API invocation must carry a signature. The TOP server verifies the signature based on request parameters and rejects the request carrying an invalid signature. Currently, the TOP supports two signature algorithms: MD5(sign_method=md5) and HMAC_MD5(sign_method=hmac). The signature process is generally described as follows:
Note: Both MD5 and HMAC_MD5 digest algorithms produce a 128-bit value represented in hexadecimal. As each hexadecimal character represents four bits, the length of the signature character string is fixed to 32 hexadecimal characters.
public static String signTopRequest(Map<String, String> params, String secret, String signMethod) throws IOException { //Step 1: Check whether parameters have been sorted. String[] keys = params.keySet().toArray(new String[0]); Arrays.sort(keys); //Step 2: Splice all sorted parameter names and values together. StringBuilder query = new StringBuilder(); if (Constants.SIGN_METHOD_MD5.equals(signMethod)) { query.append(secret); } for (String key : keys) { String value = params.get(key); if (StringUtils.areNotEmpty(key, value)) { query.append(key).append(value); } } //Step 3: Use the MD5 or HMAC_MD5 algorithm to encrypt the spliced character string. byte[] bytes; if (Constants.SIGN_METHOD_HMAC.equals(signMethod)) { bytes = encryptHMAC(query.toString(), secret); } else { query.append(secret); bytes = encryptMD5(query.toString()); } //Step 4: Convert binary characters into capitalized hexadecimal characters. (A correct signature must be a character string consisting of 32 capitalized hexadecimal characters. This step is performed as required.) //return byte2hex(bytes); } public static byte[] encryptHMAC(String data, String secret) throws IOException { byte[] bytes = null; try { SecretKey secretKey = new SecretKeySpec(secret.getBytes(Constants.CHARSET_UTF8), "HmacMD5"); Mac mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); bytes = mac.doFinal(data.getBytes(Constants.CHARSET_UTF8)); } catch (GeneralSecurityException gse) { throw new IOException(gse.toString()); } return bytes; } public static byte[] encryptMD5(String data) throws IOException { return encryptMD5(data.getBytes(Constants.CHARSET_UTF8)); } 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.toUpperCase()); } return sign.toString(); }
public static string SignTopRequest(IDictionary<string, string> parameters, string secret, string signMethod) { //Step 1: Sort the dictionary based on the alphabetical order of Key. IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters, StringComparer.Ordinal); IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator(); //Step 2: Splice all sorted parameter names and values together. StringBuilder query = new StringBuilder(); if (Constants.SIGN_METHOD_MD5.Equals(signMethod)) { query.Append(secret); } while (dem.MoveNext()) { string key = dem.Current.Key; string value = dem.Current.Value; if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value)) { query.Append(key).Append(value); } } //Step 3: Use the MD5 or HMAC_MD5 algorithm to encrypt the spliced character string. byte[] bytes; if (Constants.SIGN_METHOD_HMAC.Equals(signMethod)) { HMACMD5 hmac = new HMACMD5(Encoding.UTF8.GetBytes(secret)); bytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(query.ToString())); } else { query.Append(secret); MD5 md5 = MD5.Create(); bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(query.ToString())); } //Step 4: Convert binary characters into capitalized hexadecimal characters. StringBuilder result = new StringBuilder(); for (int i = 0; i < bytes.Length; i++) { result.Append(bytes[i].ToString("X2")); } return result.ToString(); }
For details about signature sample code in other languages, see source code of the TOP official SDK.
The following uses the API aliexpress.solution.order.fulfill(order fullfillment) as an example. The procedure for invoking this API is as follows:
Public parameters:
Service parameters:
app_key12345678formatjsonlogisitics_noES2019COM0000123456methodaliexpress.solution.order.fulfillout_ref1000006270175804send_typeallservice_namSPAIN_LOCAL_CORREOSesessiontestsign_methodmd5timestamp2019-01-01 12:00:00v2.0
Assume that secret of the application is helloworld. The obtained signature is as follows: hex(md5(helloworld+Sorted and spliced parameter names and values+helloworld)) = “F7A5E0B28DEFFE9E1E6E5C0E8B0530EC”().
Perform URL encoding for all parameter names and values based on UTF-8 encoding. The sequence of parameters can be random, but the sign parameter must be included. Then, use the GET or POST method (containing parameters of the byte[] type) to initiate the HTTP request. For example:
http://gw.api.taobao.com/router/rest?method=aliexpress.solution.order.fulfill&app_key=12345678&session=test×tamp=2019-01-01 12:00:00&format=json&v=2.0&sign_method=md5&logistics_no=ES2019COM0000123456&out_ref=1000006270175804&send_type=all&service_name=SPAIN_LOCAL_CORREOS&sign=F7A5E0B28DEFFE9E1E6E5C0E8B0530EC