我正在尝试通过Coinbase Exchange私有API进行身份验证,以获取余额并下达/取消订单.下面是我正在尝试使用的代码的演示版本.我已经关注了文档但是我一直收到以下错误:
{"message":"invalid signature"}
有人可以告诉我我做错了什么吗? 🙂
编辑:我已根据Sašo的答案修改了以下代码,所以它现在有效.
<?php
$key = "";
$secret = "";
$passphrase = "";
$time = time();
$url = "https://api.gdax.com/accounts";
$data = $time."GET"."/accounts";
echo $data . "<br/>";
$sign = base64_encode(hash_hmac("sha256", $data, base64_decode($secret), true));
echo $sign . "<br/>";
$headers = array(
'CB-ACCESS-KEY: '.$key,
'CB-ACCESS-SIGN: '.$sign,
'CB-ACCESS-TIMESTAMP: '.$time,
'CB-ACCESS-PASSPHRASE: '.$passphrase,
'Content-Type: application/json'
);
var_dump($headers);
echo $url;
static $ch = null;
if (is_null($ch)) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$res = curl_exec($ch);
echo $res;
}
最佳答案 在执行hash_hmac时,您缺少raw_output的true参数
http://php.net/manual/en/function.hash-hmac.php
raw_output:设置为TRUE时,输出原始二进制数据. FALSE输出小写的十六进制.