The Android API signature means that when the API is called, a string needs to be generated according to the agreed parameters. After receiving it, the other party will verify the parameters, accept the request and return the result if it is legal.
The valid access URLs of all Android-side APIs include the following three parts:
1. Resource access path, such as /v1/deal/find_deals;
2 . Request parameters: that is, the required parameter name and parameter value param=value corresponding to the API, multiple request parameters are connected with &
For example, deal_id=1-85462&appkey=00000;
3 . Signature string, generated by signature algorithm
The signature algorithm is as follows:
1. Arrange all request parameters except appkey in ascending dictionary order;
2. Concatenate the above sorted parameter table with strings, such as key1value1key2value2key3value3...keyNvalueN;
3. Use app key as the prefix and app secret as the suffix, and perform SHA-1 calculation on the string. and converted into hexadecimal encoding;
4. After converting to all uppercase, the signature string is obtained
After the signature string is obtained, append it to the corresponding URL as the sign parameter , you can access the API normally.
Note: Please ensure that the HTTP request data encoding must be in UTF-8 format, and the URL must also be in UTF-8 encoding format.
Reference code:
// Define the appKey and appSecret obtained by application
String appkey = "XXXXXXXX";
String secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
String apiUrl = "/v1/business/find_businesses";
// Create parameter table
Map
paramMap.put("format", "json");
paramMap.put("city", "Shanghai");
paramMap.put("latitude", "31.21524");
paramMap.put("longitude", "121.420033");
paramMap.put ("category", "Gourmet");
paramMap.put("region", "Changning District");
paramMap.put("limit", "20") ;
paramMap.put("radius", "2000");
paramMap.put("offset_type", "0");
paramMap. put("has_coupon", "1");
paramMap.put("has_deal", "1");
paramMap.put("keyword", "Thai food" );
paramMap.put("sort", "7");
// Dictionary sorting of parameter names
String[] keyArray = paramMap .keySet().toArray(new String[0]);
Arrays.sort(keyArray);
// Splicing ordered parameter name-value strings
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(appkey);
for (String key : keyArray)
{
stringBuilder.append(key).append(paramMap.get(key));
}
stringBuilder.append(secret);
String codes = stringBuilder.toString();
// String connection example
// XXXXXXXXcategory Food city Shanghai formatjsonhas_coupon1has_deal1keyword Thai foodlatitude31.21524limit20longitude121.420033offset_type0radius2000regionChangning Districtsort7XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//SHA-1 encoding, the Apache codec is used here to obtain the signature (shaHex() will first convert Chinese to UTF8 encoding and then perform sha1 calculation. When using other tool packages, please pay attention to UTF8 encoding conversion) < /p>
/*
* The following sha1 signature code has the same effect
* byte[] sha = org.apache.commons.codec.digest.DigestUtils.sha(org .apache.commons.codec.binary.StringUtils.getBytesUtf8(codes));
* String sign = org.apache.commons.codec.binary.Hex.encodeHexString(sha).toUpperCase(); < /p>
*/
String sign = org.apache.commons.codec.digest.DigestUtils.shaHex(codes).toUpperCase();
//Signature example
//7D78381BC58E1DB1DBA4BD965916FE6B4D5DC892