尝试出售时签名无效

时间:2019-08-27 19:57:43

标签: coinbase-api

我正在尝试通过API从特定钱包中出售我的比特币。

我的GET调用正常工作,但POST调用显示“无效签名”

$API_KEY        = '*****';
$API_SECRET = '*****';

$data['amount'] = 10;
$data['currency']   = 'USD';
$data['quote']      = true;

$api_url    = '/v2/accounts/593b0285-****-****-****-f0ddcb6c9ccc/sells';
$timestamp  = time();
$signature  = hash_hmac('SHA256',$timestamp.'POST'.$api_url.json_encode($data),$API_SECRET;
$version    = '2017-11-11';
$headers    = array('CB-ACCESS-SIGN: '.$signature,
                   'CB-ACCESS-TIMESTAMP: '.$timestamp,
                   'CB-ACCESS-KEY: '.$API_KEY,
                   'CB-VERSION: '.$version);

$ch     = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://api.coinbase.com'.$api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$data   = curl_exec($ch);

这是我从Coinbase获得的结果

stdClass Object
(
    [errors] => Array
        (
            [0] => stdClass Object
                (
                    [id] => authentication_error
                    [message] => invalid signature
                )
        )
)

我已将货币更改为BTC进行尝试。 $ api_url中的帐户ID是我执行帐户API调用时的帐户ID。

[id] => 593b0285-****-****-****-f0ddcb6c9ccc
[name] => BTC Wallet
[primary] => 1
[type] => wallet

1 个答案:

答案 0 :(得分:0)

我发现,我的标题中缺少 Content-Type

$headers    = array('CB-ACCESS-SIGN: '.$signature,
                    'CB-ACCESS-TIMESTAMP: '.$timestamp,
                    'CB-ACCESS-KEY: '.$API_KEY,
                    'CB-VERSION: '.$version,
                    "Content-Type: application/json");

现在一切正常