請(qǐng)求簽名
為了保證請(qǐng)求的完整性和安全性,防止數(shù)據(jù)被篡改或偽造,對(duì)于每一次HTTP或者HTTPS協(xié)議請(qǐng)求,阿里云視覺(jué)智能開(kāi)放平臺(tái)通過(guò)AccessKey ID和AccessKey Secret對(duì)稱(chēng)加密方式,根據(jù)訪(fǎng)問(wèn)中的簽名信息驗(yàn)證請(qǐng)求訪(fǎng)問(wèn)者的身份。其中,AccessKey ID是訪(fǎng)問(wèn)者身份,AccessKey Secret是加密簽名字符串和服務(wù)器端驗(yàn)證簽名字符串的密鑰,必須嚴(yán)格保密,謹(jǐn)防泄露。
阿里云視覺(jué)智能開(kāi)放平臺(tái)各類(lèi)目視覺(jué)AI能力API接入、接口使用或問(wèn)題咨詢(xún)等,請(qǐng)通過(guò)釘釘群(23109592)加入阿里云視覺(jué)智能開(kāi)放平臺(tái)咨詢(xún)?nèi)郝?lián)系我們。
簽名流程
指定請(qǐng)求參數(shù)。
在代碼中指定請(qǐng)求參數(shù),參數(shù)中需要包含公共請(qǐng)求頭和接口必備的參數(shù)信息。
說(shuō)明請(qǐng)求參數(shù)中不允許出現(xiàn)以Signature為key的參數(shù)。
示例代碼如下:
// 創(chuàng)建AccessKey ID和AccessKey Secret,請(qǐng)參見(jiàn):http://bestwisewords.com/document_detail/175144.html // 如果您使用的是RAM用戶(hù)的AccessKey,還需要為子賬號(hào)授予權(quán)限AliyunVIAPIFullAccess,請(qǐng)參見(jiàn):http://bestwisewords.com/document_detail/145025.html // 從環(huán)境變量讀取配置的AccessKey ID和AccessKey Secret。運(yùn)行代碼示例前必須先配置環(huán)境變量。 String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"); String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"); java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));// 這里一定要設(shè)置GMT時(shí)區(qū) java.util.Map<String, String> params = new java.util.HashMap<String, String>();
指定參數(shù):
params.put("SignatureMethod", "HMAC-SHA1"); params.put("SignatureNonce", java.util.UUID.randomUUID().toString()); params.put("AccessKeyId", accessKeyId); params.put("SignatureVersion", "1.0"); params.put("Timestamp", df.format(new java.util.Date())); params.put("Format", "JSON"); params.put("RegionId", "cn-shanghai"); // API版本,與類(lèi)目相關(guān),具體類(lèi)目的API版本請(qǐng)參考:http://bestwisewords.com/document_detail/464194.html params.put("Version", "2019-09-30"); // API Action,能力名稱(chēng),請(qǐng)參考具體算法文檔詳情頁(yè)中的Action參數(shù),這里以圖像超分為例:http://bestwisewords.com/document_detail/151947.html params.put("Action", "MakeSuperResolutionImage"); // 下面是業(yè)務(wù)參數(shù),業(yè)務(wù)參數(shù)指的是具體算法文檔請(qǐng)求參數(shù)中除了Action參數(shù)之外的參數(shù) // 注意,使用簽名機(jī)制調(diào)用,文件參數(shù)目前僅支持上海OSS鏈接,可參考http://bestwisewords.com/document_detail/175142.html文檔將文件放入上海OSS中。如果是其他情況(如本地文件或者其他鏈接),請(qǐng)先顯式地轉(zhuǎn)換成上海OSS鏈接,可參考http://bestwisewords.com/document_detail/155645.html文檔中的方式二,但該方案不支持web前端環(huán)境直接調(diào)用。 params.put("Url", "http://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/MakeSuperResolution/sup-dog.png"); // 對(duì)于數(shù)組類(lèi)字段,需要將參數(shù)中的N改為從1開(kāi)始的連續(xù)的具體數(shù)字,比如參數(shù)Tasks.N.ImageURL,需要輸入兩張圖片,則為 // params.put("Tasks.1.ImageURL", "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectLivingFace/DetectLivingFace11.jpg"); // params.put("Tasks.2.ImageURL", "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectLivingFace/DetectLivingFace13.jpg");
去除簽名關(guān)鍵字Key:
if (paras.containsKey("Signature")) { paras.remove("Signature"); }
根據(jù)參數(shù)Key排序(順序)。
參考代碼如下:
java.util.TreeMap<String, String> sortParas = new java.util.TreeMap<String, String>(); sortParas.putAll(paras);
構(gòu)造待簽名的請(qǐng)求串。
首先介紹下面會(huì)用到的特殊URL編碼,這個(gè)是POP特殊的一種規(guī)則,即在一般的URLEncode后再增加如下三種字符替換。
加號(hào)
+
替換為%20
星號(hào)
*
替換為%2A
波浪號(hào)
~
替換為%7E
參考代碼如下:
public static String specialUrlEncode(String value) throws Exception { return java.net.URLEncoder.encode(value, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("~", "%7E"); }
構(gòu)造待簽名的請(qǐng)求串,把排序后的參數(shù)順序拼接成如下格式:
"&" + specialUrlEncode(參數(shù)Key) + "=" + specialUrlEncode(參數(shù)值)
參考代碼如下:
java.util.Iterator<String> it = sortParas.keySet().iterator(); StringBuilder sortQueryStringTmp = new StringBuilder(); while (it.hasNext()) { String key = it.next(); sortQueryStringTmp.append("&").append(specialUrlEncode(key)).append("=").append(specialUrlEncode(paras.get(key))); } String sortedQueryString = sortQueryStringTmp.substring(1);// 去除第一個(gè)多余的&符號(hào)
打印上面的sortQueryString。結(jié)果如下:
AccessKeyId=yourAccessId&Action=MakeSuperResolutionImage&Format=JSON&RegionId=cn-shanghai&SignatureMethod=HMAC-SHA1&SignatureNonce=4a816d44-6186-4f7e-a45f-ba1b3ed73aed&SignatureVersion=1.0&Timestamp=2019-12-07T13%3A28%3A52Z&Url=http%3A%2F%2Fviapi-demo.oss-cn-shanghai.aliyuncs.com%2Fviapi-demo%2Fimages%2FMakeSuperResolution%2Fsup-dog.png&Version=2019-09-30
對(duì)應(yīng)的未進(jìn)行URL編碼的值(方便進(jìn)行對(duì)比):
AccessKeyId=yourAccessId&Action=MakeSuperResolutionImage&Format=JSON&RegionId=cn-shanghai&SignatureMethod=HMAC-SHA1&SignatureNonce=4a816d44-6186-4f7e-a45f-ba1b3ed73aed&SignatureVersion=1.0&Timestamp=2019-12-07T13:28:52Z&Url=http://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/MakeSuperResolution/sup-dog.png&Version=2019-09-30
按POP的簽名規(guī)則拼接成最終的待簽名串。規(guī)則如下:
HTTPMethod + “&” + specialUrlEncode(“/”) + ”&” + specialUrlEncode(sortedQueryString)
參考代碼如下:
StringBuilder stringToSign = new StringBuilder(); stringToSign.append("POST").append("&"); stringToSign.append(specialUrlEncode("/")).append("&"); stringToSign.append(specialUrlEncode(sortedQueryString));
這就完成了待簽名的請(qǐng)求字符串。結(jié)果如下:
POST&%2F&AccessKeyId%3DyourAccessId&Action%3DMakeSuperResolutionImage&Format%3DJSON&RegionId%3Dcn-shanghai&SignatureMethod%3DHMAC-SHA1&SignatureNonce%3D4a816d44-6186-4f7e-a45f-ba1b3ed73aed&SignatureVersion%3D1.0&Timestamp%3D2019-12-07T13%253A28%253A52Z&Url%3Dhttp%253A%252F%252Fviapi-demo.oss-cn-shanghai.aliyuncs.com%252Fviapi-demo%252Fimages%252FMakeSuperResolution%252Fsup-dog.png&Version%3D2019-09-30
進(jìn)行簽名。
簽名采用HmacSHA1算法+Base64,編碼采用UTF-8。使用AccessKeySecret進(jìn)行加密,參考代碼如下:
String sign = sign(accessSecret + "&", stringToSign.toString()); public static String sign(String accessSecret, String stringToSign) throws Exception { javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA1"); mac.init(new javax.crypto.spec.SecretKeySpec(accessSecret.getBytes("UTF-8"), "HmacSHA1")); byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8")); return new sun.misc.BASE64Encoder().encode(signData); }
參數(shù)
說(shuō)明
accessSecret
您的AccessKeyID對(duì)應(yīng)的密鑰AccessKeySecret。
說(shuō)明POP要求后面多加一個(gè)and(&)字符,即accessSecret + “&”。
stringToSign
第三步生成的待簽名請(qǐng)求串。
簽名后的結(jié)果如下:
poMnQhB2W5xndjcsW5VZjSdkvnU=
增加簽名結(jié)果到請(qǐng)求參數(shù)中,發(fā)送請(qǐng)求。
String Signature = specialUrlEncode(sign);// poMnQhB2W5xndjcsW5VZjSdkvnU%3D
說(shuō)明簽名也要做特殊URL編碼。
最終完整的POST請(qǐng)求HTTP為:
http://imageenhan.cn-shanghai.aliyuncs.com/?Signature=poMnQhB2W5xndjcsW5VZjSdkvnU%3D&AccessKeyId=yourAccessId&Action=MakeSuperResolutionImage&Format=JSON&RegionId=cn-shanghai&SignatureMethod=HMAC-SHA1&SignatureNonce=4a816d44-6186-4f7e-a45f-ba1b3ed73aed&SignatureVersion=1.0&Timestamp=2019-12-07T13%3A28%3A52Z&Url=http%3A%2F%2Fviapi-demo.oss-cn-shanghai.aliyuncs.com%2Fviapi-demo%2Fimages%2FMakeSuperResolution%2Fsup-dog.png&Version=2019-09-30
說(shuō)明imageenhan.cn-shanghai.aliyuncs.com是圖像生產(chǎn)的訪(fǎng)問(wèn)域名,不同類(lèi)目的訪(fǎng)問(wèn)域名是不同的,具體類(lèi)目的API訪(fǎng)問(wèn)域名,請(qǐng)參見(jiàn)訪(fǎng)問(wèn)域名。
JAVA示例
簡(jiǎn)單參數(shù)示例
以圖像超分(MakeSuperResolutionImage)為例,完整的Java簽名代碼示例如下。
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class SignDemo {
// HTTPMethod推薦使用POST
static final String API_HTTP_METHOD = "POST";
// API訪(fǎng)問(wèn)域名,與類(lèi)目相關(guān),具體類(lèi)目的API訪(fǎng)問(wèn)域名請(qǐng)參考:http://bestwisewords.com/document_detail/143103.html
static final String API_ENDPOINT = "imageenhan.cn-shanghai.aliyuncs.com";
// API版本,與類(lèi)目相關(guān),具體類(lèi)目的API版本請(qǐng)參考:http://bestwisewords.com/document_detail/464194.html
static final String API_VERSION = "2019-09-30";
static final java.text.SimpleDateFormat DF = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
public static void main(String[] args) throws Exception {
// 創(chuàng)建AccessKey ID和AccessKey Secret,請(qǐng)參見(jiàn):http://bestwisewords.com/document_detail/175144.html
// 如果您使用的是RAM用戶(hù)的AccessKey,還需要為子賬號(hào)授予權(quán)限AliyunVIAPIFullAccess,請(qǐng)參見(jiàn):http://bestwisewords.com/document_detail/145025.html
// 從環(huán)境變量讀取配置的AccessKey ID和AccessKey Secret。運(yùn)行代碼示例前必須先配置環(huán)境變量。
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessSecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
DF.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));// 這里一定要設(shè)置GMT時(shí)區(qū)
// 業(yè)務(wù)參數(shù)名字是大駝峰
Map<String, String> params = new HashMap<>();
// 注意,使用簽名機(jī)制調(diào)用,文件參數(shù)目前僅支持上海OSS鏈接,可參考http://bestwisewords.com/document_detail/175142.html文檔將文件放入上海OSS中。如果是其他情況(如本地文件或者其他鏈接),請(qǐng)先顯式地轉(zhuǎn)換成上海OSS鏈接,可參考http://bestwisewords.com/document_detail/155645.html文檔中的方式二,但該方案不支持web前端環(huán)境直接調(diào)用。
params.put("Url", "http://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/MakeSuperResolution/sup-dog.png");
// API Action,能力名稱(chēng),請(qǐng)參考具體算法文檔詳情頁(yè)中的Action參數(shù),這里以圖像超分為例:http://bestwisewords.com/document_detail/151947.html
String action = "MakeSuperResolutionImage";
execute(action, accessKeyId, accessSecret, params);
}
public static void execute(String action, String accessKeyId, String accessSecret, Map<String, String> bizParams) throws Exception {
java.util.Map<String, String> params = new java.util.HashMap<String, String>();
// 1. 系統(tǒng)參數(shù)
params.put("SignatureMethod", "HMAC-SHA1");
params.put("SignatureNonce", java.util.UUID.randomUUID().toString());//防止重放攻擊
params.put("AccessKeyId", accessKeyId);
params.put("SignatureVersion", "1.0");
params.put("Timestamp", DF.format(new java.util.Date()));
params.put("Format", "JSON");
// 2. 業(yè)務(wù)API參數(shù)
params.put("RegionId", "cn-shanghai");
params.put("Version", API_VERSION);
params.put("Action", action);
if (bizParams != null && !bizParams.isEmpty()) {
params.putAll(bizParams);
}
// 3. 去除簽名關(guān)鍵字Key
if (params.containsKey("Signature")) {
params.remove("Signature");
}
// 4. 參數(shù)KEY排序
java.util.TreeMap<String, String> sortParams = new java.util.TreeMap<String, String>();
sortParams.putAll(params);
// 5. 構(gòu)造待簽名的字符串
java.util.Iterator<String> it = sortParams.keySet().iterator();
StringBuilder sortQueryStringTmp = new StringBuilder();
while (it.hasNext()) {
String key = it.next();
sortQueryStringTmp.append("&").append(specialUrlEncode(key)).append("=").append(specialUrlEncode(params.get(key)));
}
String sortedQueryString = sortQueryStringTmp.substring(1);// 去除第一個(gè)多余的&符號(hào)
StringBuilder stringToSign = new StringBuilder();
stringToSign.append(API_HTTP_METHOD).append("&");
stringToSign.append(specialUrlEncode("/")).append("&");
stringToSign.append(specialUrlEncode(sortedQueryString));
String sign = sign(accessSecret + "&", stringToSign.toString());
// 6. 簽名最后也要做特殊URL編碼
String signature = specialUrlEncode(sign);
System.out.println(params.get("SignatureNonce"));
System.out.println("\r\n=========\r\n");
System.out.println(params.get("Timestamp"));
System.out.println("\r\n=========\r\n");
System.out.println(sortedQueryString);
System.out.println("\r\n=========\r\n");
System.out.println(stringToSign.toString());
System.out.println("\r\n=========\r\n");
System.out.println(sign);
System.out.println("\r\n=========\r\n");
System.out.println(signature);
System.out.println("\r\n=========\r\n");
// 最終生成出合法請(qǐng)求的URL
System.out.println("http://" + API_ENDPOINT + "/?Signature=" + signature + sortQueryStringTmp);
// 添加直接做post請(qǐng)求的方法
try {
// 使用生成的 URL 創(chuàng)建POST請(qǐng)求
URIBuilder builder = new URIBuilder("http://" + API_ENDPOINT + "/?Signature=" + signature + sortQueryStringTmp);
URI uri = builder.build();
HttpPost request = new HttpPost(uri);
HttpClient httpclient = HttpClients.createDefault();
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println(EntityUtils.toString(entity));
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static String specialUrlEncode(String value) throws Exception {
return java.net.URLEncoder.encode(value, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
}
public static String sign(String accessSecret, String stringToSign) throws Exception {
javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA1");
mac.init(new javax.crypto.spec.SecretKeySpec(accessSecret.getBytes("UTF-8"), "HmacSHA1"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
return new sun.misc.BASE64Encoder().encode(signData);
}
}
數(shù)組參數(shù)示例
以人臉活體檢測(cè)(DetectLivingFace)為例,完整的Java簽名代碼示例如下。
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
public class SignDemo {
// HTTPMethod推薦使用POST
static final String API_HTTP_METHOD = "POST";
// API訪(fǎng)問(wèn)域名,與類(lèi)目相關(guān),具體類(lèi)目的API訪(fǎng)問(wèn)域名請(qǐng)參考:http://bestwisewords.com/document_detail/143103.html
static final String API_ENDPOINT = "facebody.cn-shanghai.aliyuncs.com";
// API版本,與類(lèi)目相關(guān),具體類(lèi)目的API版本請(qǐng)參考:http://bestwisewords.com/document_detail/464194.html
static final String API_VERSION = "2019-12-30";
static final java.text.SimpleDateFormat DF = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
public static void main(String[] args) throws Exception {
// 創(chuàng)建AccessKey ID和AccessKey Secret,請(qǐng)參見(jiàn):http://bestwisewords.com/document_detail/175144.html
// 如果您使用的是RAM用戶(hù)的AccessKey,還需要為子賬號(hào)授予權(quán)限AliyunVIAPIFullAccess,請(qǐng)參見(jiàn):http://bestwisewords.com/document_detail/145025.html
// 從環(huán)境變量讀取配置的AccessKey ID和AccessKey Secret。運(yùn)行代碼示例前必須先配置環(huán)境變量。
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessSecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
DF.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));// 這里一定要設(shè)置GMT時(shí)區(qū)
// 業(yè)務(wù)參數(shù)名字是大駝峰
Map<String, String> params = new HashMap<>();
// 注意,使用簽名機(jī)制調(diào)用,文件參數(shù)目前僅支持上海OSS鏈接,可參考http://bestwisewords.com/document_detail/175142.html文檔將文件放入上海OSS中。如果是其他情況(如本地文件或者其他鏈接),請(qǐng)先顯式地轉(zhuǎn)換成上海OSS鏈接,可參考http://bestwisewords.com/document_detail/155645.html文檔中的方式二,但該方案不支持web前端環(huán)境直接調(diào)用。
// 對(duì)于數(shù)組類(lèi)字段,需要將參數(shù)中的N改為從1開(kāi)始的連續(xù)的具體數(shù)字,比如參數(shù)Tasks.N.ImageURL,需要輸入兩張圖片,則為
params.put("Tasks.1.ImageURL", "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectLivingFace/DetectLivingFace11.jpg");
params.put("Tasks.2.ImageURL", "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/facebody/DetectLivingFace/DetectLivingFace13.jpg");
// API Action,能力名稱(chēng),請(qǐng)參考具體算法文檔詳情頁(yè)中的Action參數(shù),這里以人臉活體檢測(cè)為例:http://bestwisewords.com/document_detail/151947.html
String action = "DetectLivingFace";
execute(action, accessKeyId, accessSecret, params);
}
public static void execute(String action, String accessKeyId, String accessSecret, Map<String, String> bizParams) throws Exception {
java.util.Map<String, String> params = new java.util.HashMap<String, String>();
// 1. 系統(tǒng)參數(shù)
params.put("SignatureMethod", "HMAC-SHA1");
params.put("SignatureNonce", java.util.UUID.randomUUID().toString());//防止重放攻擊
params.put("AccessKeyId", accessKeyId);
params.put("SignatureVersion", "1.0");
params.put("Timestamp", DF.format(new java.util.Date()));
params.put("Format", "JSON");
// 2. 業(yè)務(wù)API參數(shù)
params.put("RegionId", "cn-shanghai");
params.put("Version", API_VERSION);
params.put("Action", action);
if (bizParams != null && !bizParams.isEmpty()) {
params.putAll(bizParams);
}
// 3. 去除簽名關(guān)鍵字Key
if (params.containsKey("Signature")) {
params.remove("Signature");
}
// 4. 參數(shù)KEY排序
java.util.TreeMap<String, String> sortParams = new java.util.TreeMap<String, String>();
sortParams.putAll(params);
// 5. 構(gòu)造待簽名的字符串
java.util.Iterator<String> it = sortParams.keySet().iterator();
StringBuilder sortQueryStringTmp = new StringBuilder();
while (it.hasNext()) {
String key = it.next();
sortQueryStringTmp.append("&").append(specialUrlEncode(key)).append("=").append(specialUrlEncode(params.get(key)));
}
String sortedQueryString = sortQueryStringTmp.substring(1);// 去除第一個(gè)多余的&符號(hào)
StringBuilder stringToSign = new StringBuilder();
stringToSign.append(API_HTTP_METHOD).append("&");
stringToSign.append(specialUrlEncode("/")).append("&");
stringToSign.append(specialUrlEncode(sortedQueryString));
String sign = sign(accessSecret + "&", stringToSign.toString());
// 6. 簽名最后也要做特殊URL編碼
String signature = specialUrlEncode(sign);
System.out.println(params.get("SignatureNonce"));
System.out.println("\r\n=========\r\n");
System.out.println(params.get("Timestamp"));
System.out.println("\r\n=========\r\n");
System.out.println(sortedQueryString);
System.out.println("\r\n=========\r\n");
System.out.println(stringToSign.toString());
System.out.println("\r\n=========\r\n");
System.out.println(sign);
System.out.println("\r\n=========\r\n");
System.out.println(signature);
System.out.println("\r\n=========\r\n");
// 最終生成出合法請(qǐng)求的URL
System.out.println("http://" + API_ENDPOINT + "/?Signature=" + signature + sortQueryStringTmp);
// 添加直接做post請(qǐng)求的方法
try {
// 使用生成的 URL 創(chuàng)建POST請(qǐng)求
URIBuilder builder = new URIBuilder("http://" + API_ENDPOINT + "/?Signature=" + signature + sortQueryStringTmp);
URI uri = builder.build();
HttpPost request = new HttpPost(uri);
HttpClient httpclient = HttpClients.createDefault();
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println(EntityUtils.toString(entity));
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static String specialUrlEncode(String value) throws Exception {
return java.net.URLEncoder.encode(value, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
}
public static String sign(String accessSecret, String stringToSign) throws Exception {
javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA1");
mac.init(new javax.crypto.spec.SecretKeySpec(accessSecret.getBytes("UTF-8"), "HmacSHA1"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
return new sun.misc.BASE64Encoder().encode(signData);
}
}
PHP示例
簡(jiǎn)單參數(shù)示例
以圖像超分(MakeSuperResolutionImage)為例,完整的PHP簽名代碼示例如下。
<?php
date_default_timezone_set("GMT");// 這里一定要設(shè)置GMT時(shí)區(qū)
class SignDemo
{
// HTTPMethod推薦使用POST
const API_HTTP_METHOD = "POST";
// API訪(fǎng)問(wèn)域名,與類(lèi)目相關(guān),具體類(lèi)目的API訪(fǎng)問(wèn)域名請(qǐng)參考:http://bestwisewords.com/document_detail/143103.html
const API_ENDPOINT = "imageenhan.cn-shanghai.aliyuncs.com";
// API版本,與類(lèi)目相關(guān),具體類(lèi)目的API版本請(qǐng)參考:http://bestwisewords.com/document_detail/464194.html
const API_VERSION = "2019-09-30";
const DF = "Y-m-d\TH:i:s\Z";
private $accessKeyId;
private $accessSecret;
public function __construct($accessKeyId, $accessSecret) {
$this->accessKeyId = $accessKeyId;
$this->accessSecret = $accessSecret;
}
public function execute($action, $bizParams) {
$params = array(
// 1. 系統(tǒng)參數(shù)
"SignatureMethod" => "HMAC-SHA1",
"SignatureNonce" => uniqid(),
"AccessKeyId" => $this->accessKeyId,
"SignatureVersion" => "1.0",
"Timestamp" => gmdate(self::DF),
"Format" => "JSON",
// 2. 業(yè)務(wù)API參數(shù)
"RegionId" => "cn-shanghai",
"Version" => self::API_VERSION,
"Action" => $action
);
if (!empty($bizParams)) {
$params = array_merge($params, $bizParams);
}
unset($params["Signature"]);
ksort($params);
// 3. 構(gòu)造待簽名的字符串
$sortedQueryStringTmp = "";
foreach ($params as $key => $value) {
$sortedQueryStringTmp .= "&" . $this->specialUrlEncode($key) . "=" . $this->specialUrlEncode($value);
}
$sortedQueryString = substr($sortedQueryStringTmp, 1);// 去除第一個(gè)多余的&符號(hào)
// 4. 簽名最后也要做特殊URL編碼
$stringToSign = self::API_HTTP_METHOD . "&" . $this->specialUrlEncode("/") . "&" . $this->specialUrlEncode($sortedQueryString);
$signature = $this->specialUrlEncode($this->sign($this->accessSecret . "&", $stringToSign));
// 5、最終生成出合法請(qǐng)求的URL
$requestUrl = "http://" . self::API_ENDPOINT . "/?Signature=" . $signature . $sortedQueryStringTmp;
echo $requestUrl . "\n";
try {
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'ignore_errors' => true,
)
));
$response = file_get_contents($requestUrl, false, $context);
echo $response;
} catch (Exception $e) {
echo $e->getMessage();
}
}
private function specialUrlEncode($value) {
return str_replace(array("+", "*", "%7E"), array("%20", "%2A", "~"), rawurlencode($value));
}
private function sign($accessSecret, $stringToSign) {
return base64_encode(hash_hmac("sha1", $stringToSign, $accessSecret, true));
}
}
// 業(yè)務(wù)參數(shù)名字是大駝峰
// 注意,使用簽名機(jī)制調(diào)用,文件參數(shù)目前僅支持上海OSS鏈接,可參考http://bestwisewords.com/document_detail/175142.html文檔將文件放入上海OSS中。如果是其他情況(如本地文件或者其他鏈接),請(qǐng)先顯式地轉(zhuǎn)換成上海OSS鏈接,可參考http://bestwisewords.com/document_detail/155645.html文檔中的方式二,但該方案不支持web前端環(huán)境直接調(diào)用。
$bizParams = array("Url" => "http://viapi-demo.oss-cn-shanghai.aliyuncs.com/viapi-demo/images/MakeSuperResolution/sup-dog.png");
// API Action,能力名稱(chēng),請(qǐng)參考具體算法文檔詳情頁(yè)中的Action參數(shù),這里以圖像超分為例:http://bestwisewords.com/document_detail/151947.html
$action = "MakeSuperResolutionImage";
// 創(chuàng)建AccessKey ID和AccessKey Secret,請(qǐng)參見(jiàn):http://bestwisewords.com/document_detail/175144.html
// 如果您使用的是RAM用戶(hù)的AccessKey,還需要為子賬號(hào)授予權(quán)限AliyunVIAPIFullAccess,請(qǐng)參見(jiàn):http://bestwisewords.com/document_detail/145025.html
// 從環(huán)境變量讀取配置的AccessKey ID和AccessKey Secret。運(yùn)行代碼示例前必須先配置環(huán)境變量。
$signDemo = new SignDemo(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'));
$signDemo->execute($action, $bizParams);
?>