Getting Started
Public Parameters
Arcvideo Cloud's API (exclude Upload API) contains five public parameters: action, accessKey, version, timestamp and signature.
Parameter | Description |
---|---|
action | The name of the API |
accessKey | The AccessKey of assigned to the user. |
version | The api version |
timestamp | The invoke time in milliseconds since january 1 1970. |
signature | The signature according to the Signature Rule below |
Signature Generation
The signature string is generated by the following steps:
- Generate a string by sorting all parameters by name (CASE-INSENSITIVE-ORDER) and add accessSecret to the string header;
- Call the algorithm HmacSHA256 to generate a signature for the string;
- Each api generates a different signature based on the input parameters
Example
Let's use API: Get Account Information as an example to illustrate the generation of signatures, The five parameters of this interface are accessKey, action, version, timestamp, and signature: accessSecret = "5GcXHNYdAVVdFW0yervG"
accessKey = "a020e193-0f1"
action = "getUser"
version = "2.0"
timestamp= "1466488681033"
then the signature value of the api is called:
signature = hmacSHA256Encrypt("5GcXHNYdAVVdFW0yervGaccessKey=a020e193-0f1action=getUsertimestamp=1466488681033version=2.0", "5GcXHNYdAVVdFW0yervG") = 3d864184117e240ad4def677c48fbba509a1d0d48ea5dfb9e914c587ae3ce5bf
Response Format
The API response value is in json format as follows:
{
"code": 0, // integer, error code (0 means success)
"message": "success", // string, error message or "success"
"result": ..., // string/json, return data
"success": true // boolean, true or false
}
Sample Code
Encryption
SHA256 Hmac signature generates a 64 bit string, as shown in the following code:
- Java
- PHP
- C#
public static String hmacSHA256Encrypt(String plainText, String accessSecret) throws Exception {
Mac mac = Mac.getInstance("HmacSHA256");
byte[] secretByte = accessSecret.getBytes("UTF-8");
byte[] dataBytes = plainText.getBytes("UTF-8");
SecretKey secret = new SecretKeySpec(secretByte, "HMACSHA256");
mac.init(secret);
byte[] doFinal = mac.doFinal(dataBytes);
byte[] hexB = new Hex().encode(doFinal);
String signature = new String(hexB);
return signature;
}
<?php
echo hash_hmac('sha256', $plainText, $accessSecret);
?>
private static string hmacSHA256Encrypt(string plainText, string accessSecret)
{
HMACSHA256 hash = new HMACSHA256(Encoding.UTF8.GetBytes(accessSecret));
byte[] bytes = hash.ComputeHash(Encoding.UTF8.GetBytes(plainText));
return BitConverter.ToString(bytes).Replace("-", string.Empty).ToLower();
}
Signature
- Java
- PHP
- C#
public static String generateSignature(HashMap<String, Object> param, String secret) throws Exception {
if (StringUtils.isBlank(secret) || MapUtils.isEmpty(param)) {
return null;
}
List<String> keyList = new ArrayList<String>(param.keySet());
Collections.sort(keyList, String.CASE_INSENSITIVE_ORDER);
StringBuilder sb = new StringBuilder(secret);
for (String key :keyList) {
if (param.get(key) != null && !key.equals("signature")) {
sb.append(key);
sb.append("=");
sb.append(param.get(key));
}
}
String signature = hmacSHA256Encrypt(sb.toString(), secret);
return signature;
}
function mkSig(array $arr, $accessKey, $accessSecret){
ksort($arr);
$request=$accessSecret;
foreach ($arr as $key => $value) {
$request .= $key.'='.$value;
}
echo "\n",$request,"\n";
return sha256($request, $accessSecret);
}
public static string generateSignature(IDictionary<string,object> param, string secret)
{
List<string> keys = new List<string>(param.Keys);
keys.Sort(StringComparer.OrdinalIgnoreCase);
StringBuilder sb = new StringBuilder(secret);
foreach (string key in keys)
{
sb.Append(key).Append("=").Append(param[key]);
}
return hmacSHA256Encrypt(sb.ToString(), secret);
}
Api Calls
- Java
- PHP
- C#
String ACCESSKEY = "a020e193-0f1";
String ACCESSSECRT = "5GcXHNYdAVVdFW0yervG";
String URI = "http://api-az-cn.danghongyun.com/rest?" ;
HashMap<String,String> params = new HashMap<String,String>();
params.put("accessKey", ACCESSKEY);
params.put("signature", generateSignature(params, ACCESSSECRT));
String request = createURL(URI, params);
String httpReslut = HttpUtil.get(request);
System.out.println(httpReslut);
return JSON.parseArray(httpReslut);
Url splicing:
function mkUrl(array $dataArr){
$url='';
foreach ($dataArr as $key => $value) {
$url .= $key.'='.$value.'&';
}
return substr($url, 0, strlen($url)-1);
}
Get:
function curlGet($url){
echo "\n",$url,"\n";
$ch = curl_init($url) ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
Post:
function curlPost(array $dataArr, $url){
$ch = curl\_init();
curl\_setopt($ch, CURLOPT_URL,$url);
curl\_setopt($ch, CURLOPT_POST,count($dataArr));
curl\_setopt($ch, CURLOPT_POSTFIELDS,$dataArr);
ob_start();
curl_exec($ch);
$res = ob_get_contents();
ob_end_clean();
curl_close($ch);
return $res;
}
// add parameters
SortedList<string, string> sl = new SortedList<string, string>();
sl.Add("accessKey", txtAccessKey.Text);
// caculate signature
string signature = generateSignature(sl, txtAccessSecret.Text);
sl.Add("signature", signature );
// make url
string url = createUrl(txtUri.Text, sl);
// invoke and get result
WebRequest request = WebRequest.Create(url);
try
{
WebResponse wResp = request.GetResponse();
Stream respStream = wResp.GetResponseStream();
using (StreamReader reader = new StreamReader(respStream, Encoding.UTF8))
{
Console.WriteLine(reader.ReadToEnd());
}
} catch(Exception e1) {
Console.WriteLine(e1.Message);
}