我正在开发一个移动应用程序,该应用程序将使用Gravity Forms API从WordPress安装中获取数据。
不幸的是,我总是收到:
{"status":401,"response":"Permission denied"}
我想,创建签名的方式存在错误,但我找不到任何东西。有什么提示吗?
HmacSHA1(k,d,_p,_z){
if(!_p){_p='=';}if(!_z){_z=8;}function _f(t,b,c,d){if(t<20){return(b&c)|((~b)&d);}if(t<40){return b^c^d;}if(t<60){return(b&c)|(b&d)|(c&d);}return b^c^d;}function _k(t){return(t<20)?1518500249:(t<40)?1859775393:(t<60)?-1894007588:-899497514;}function _s(x,y){var l=(x&0xFFFF)+(y&0xFFFF),m=(x>>16)+(y>>16)+(l>>16);return(m<<16)|(l&0xFFFF);}function _r(n,c){return(n<<c)|(n>>>(32-c));}function _c(x,l){x[l>>5]|=0x80<<(24-l%32);x[((l+64>>9)<<4)+15]=l;var w=[80],a=1732584193,b=-271733879,c=-1732584194,d=271733878,e=-1009589776;for(var i=0;i<x.length;i+=16){var o=a,p=b,q=c,r=d,s=e;for(var j=0;j<80;j++){if(j<16){w[j]=x[i+j];}else{w[j]=_r(w[j-3]^w[j-8]^w[j-14]^w[j-16],1);}var t=_s(_s(_r(a,5),_f(j,b,c,d)),_s(_s(e,w[j]),_k(j)));e=d;d=c;c=_r(b,30);b=a;a=t;}a=_s(a,o);b=_s(b,p);c=_s(c,q);d=_s(d,r);e=_s(e,s);}return[a,b,c,d,e];}function _b(s){var b=[],m=(1<<_z)-1;for(var i=0;i<s.length*_z;i+=_z){b[i>>5]|=(s.charCodeAt(i/8)&m)<<(32-_z-i%32);}return b;}function _h(k,d){var b=_b(k);if(b.length>16){b=_c(b,k.length*_z);}var p=[16],o=[16];for(var i=0;i<16;i++){p[i]=b[i]^0x36363636;o[i]=b[i]^0x5C5C5C5C;}var h=_c(p.concat(_b(d)),512+d.length*_z);return _c(o.concat(h),512+160);}function _n(b){var t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",s='';for(var i=0;i<b.length*4;i+=3){var r=(((b[i>>2]>>8*(3-i%4))&0xFF)<<16)|(((b[i+1>>2]>>8*(3-(i+1)%4))&0xFF)<<8)|((b[i+2>>2]>>8*(3-(i+2)%4))&0xFF);for(var j=0;j<4;j++){if(i*8+j*6>b.length*32){s+=_p;}else{s+=t.charAt((r>>6*(3-j))&0x3F);}}}return s;}function _x(k,d){return _n(_h(k,d));}return _x(k,d);
}
CalculateSig (stringToSign) {
var hash = this.HmacSHA1(stringToSign, this.privateKey, '','');
var base64 = btoa(hash);
return encodeURIComponent(base64);
}
CreateSig (method, route, future_unixtime) {
let stringToSign = this.publicKey + ":" + method + ":" + route + ":" + future_unixtime;
return this.CalculateSig(stringToSign);
}
getFutureUnixTime() {
let expiration = 3600;
let unixtime = Math.round((new Date()).getTime() / 1000);
return unixtime + expiration;
}
getData () {
let route = "forms/1/entries";
let future_unixtime = this.getFutureUnixTime();
let sig = this.CreateSig("GET", route, future_unixtime);
var url = this.domain + route + '?api_key=' + this.publicKey + '&signature=' + sig + '&expires=' + future_unixtime;
return this.http.get(url).map(res => res.json());
}
您是否尝试过只返回 url 并查看它的响应是否为 200。
我使用类似的请求
//creating request URL
$method = "GET";
$expires = strtotime("+60 mins");
$string_to_sign = sprintf("%s:%s:%s:%s", $api_key, $method, $route, $expires);
$sig = calculate_signature( $string_to_sign, $private_key );
$url = $api_url . $route . '?api_key=' . $api_key . '&signature=' . $sig . '&expires=' . $expires;
然后使用 cUrl 查询网址
$curl_response = $this->getCurl($url);
。
function getCurl($url){
//Initialize cURL.
$ch = curl_init();
//Set the URL that you want to GET by using the CURLOPT_URL option.
curl_setopt($ch, CURLOPT_URL, $url);
//Set CURLOPT_RETURNTRANSFER so that the content is returned as a variable.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Set CURLOPT_FOLLOWLOCATION to true to follow redirects.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
//Execute the request.
$data = curl_exec($ch);
//Close the cURL handle.
curl_close($ch);
//Print the data out onto the page.
$url_response = json_decode($data);
$curl_status = $url_response->status;
//forms retrieved successfully
return $url_response->response;
}
function calculate_signature($string, $private_key) {
$hash = hash_hmac("sha1", $string, $private_key, true);
$sig = rawurlencode(base64_encode($hash));
return $sig;
}