首页 > 编程语言 >aardio调用百度云人脸识别(api认证机制authorization算法)

aardio调用百度云人脸识别(api认证机制authorization算法)

时间:2022-09-18 11:38:16浏览次数:78  
标签:return string tmptab url aardio api tmptab2 var authorization

功能:调用百度云识别里的人脸识别api

 

这里同时分享给需要的人:

namespace baidu
import inet.url
import time.zone
import crypt.hmac
import crypt.bin
string = ..string
encodeUri = inet.url.encode
UriEncodeExceptSlash = function(s){
    if( utf8 === null ) utf8 = "true";
    if( !utf8 ) s = string.fromto(s);
    return ..string.hex(s,"%","-._~/");
}
CanonicalQueryStringFun = function(url){
    var tmptab = string.split(url,'?')
    if(#tmptab = 2){
        tmptab = string.split(tmptab[2],"&")
        var tmptab2,querytab = {},{}
        for(i=1;#tmptab;1){
            tmptab2 = string.split(tmptab[i],"=")
            ..table.push(querytab,encodeUri(tmptab2[1]) + "=" + encodeUri(tmptab2[[2]]:""))
        }
        ..table.sort(querytab)
        return string.join(querytab,"&")
    }
    return ""; 
}
var timestamp = tostring(time.zone.toUtc(time()))
CanonicalHeadersFun = function(url,ContentType="application/x-www-form-urlencoded"){
    var tmptab = inet.url.split(url)
    var host = tmptab.host
    ContentType = ContentType
    xbcedate = timestamp
    var tmptab2 = {}
    ..table.push(tmptab2,"host:"+encodeUri(host))
    //..table.push(tmptab2,"content-type:"+encodeUri(ContentType))
    //..table.push(tmptab2,"x-bce-date:"+encodeUri(xbcedate))
    //..table.sort(tmptab2)
    return string.join(tmptab2,'\n')
}
CanonicalRequestFun = function(url,Method="GET"){
    Method = string.upper(Method)
    var tmptab = inet.url.split(url)
    var CanonicalURI = UriEncodeExceptSlash(tmptab.path)
    var tmpstr = Method + '\n'
    tmpstr += CanonicalURI + '\n'
    tmpstr += CanonicalQueryStringFun(url) + '\n'
    tmpstr += CanonicalHeadersFun(url)
    return tmpstr; 
}
SigningKeyFun = function(ak,sk){
    return string.lower(crypt.hmac(sk,"bce-auth-v1/"+ak+"/"+timestamp+"/1800",0x800C/*_CALG_SHA_256*/).getHexValue()); 
}
SignatureFun = function(SigningKey,CanonicalRequest){
    return string.lower(crypt.hmac(SigningKey,CanonicalRequest,0x800C/*_CALG_SHA_256*/).getHexValue())
}
authorizationFun = function(url,Method,ak,sk){
    var SigningKey = SigningKeyFun(ak,sk)
    var CanonicalRequest = CanonicalRequestFun(url,Method)
    var signature = SignatureFun(SigningKey,CanonicalRequest)
    return "bce-auth-v1/"+ak+"/"+timestamp+"/1800/host/"+signature; 
}
//Access Key ID Secret Access Key 获取方式-》百度云管理中心-右上角用户中心-》安全认证
AK = "21a1c0b6ecd64e9c89d0becea18fe2c8"
SK = "3f00b409927a46ce85e981b5a8f246a1"
import console
var authorization = authorizationFun("https://aip.baidubce.com/rest/2.0/face/v1/detect","POST",AK,SK)
import inet.whttp
var whttp = inet.whttp()
var addheaders = /*
x-bce-date: [time]
Authorization: [auth]
*/
addheaders = string.replace(addheaders,"@[auth]",authorization)
addheaders = string.replace(addheaders,"@[time]",timestamp)
whttp.addHeaders = addheaders
console.varDump( whttp.post("https://aip.baidubce.com/rest/2.0/face/v1/detect","image="+ inet.url.encode( crypt.bin.encodeBase64(string.load("C:\Users\Administrator\Desktop\新建文件夹(1)\1.jpg"))),"Content-Type: application/x-www-form-urlencoded"))
console.pause();

使用的时候要注意,最下面代码里面的图片地址改为自己电脑上实际图片地址,要不然会提示错误!

标签:return,string,tmptab,url,aardio,api,tmptab2,var,authorization
From: https://www.cnblogs.com/popdes/p/16704472.html

相关文章