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

By admin at 2017-12-03 • 0人收藏 • 1519人看过

此代码由群里 Kio1 分享

他的阿蒂欧社区貌似打不开了....

功能:调用百度云识别里的人脸识别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()

;

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

blob.png

登录后方可回帖

登 录
信息栏
 私人小站

本站域名

ChengXu.XYZ

投诉联系:  popdes@126.com



快速上位机开发学习,本站主要记录了学习过程中遇到的问题和解决办法及上位机代码分享

这里主要专注于学习交流和经验分享.
纯私人站,当笔记本用的,学到哪写到哪.
如果侵权,联系 Popdes@126.com

友情链接
Aardio官方
Aardio资源网


才仁机械


网站地图SiteMap

Loading...