首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >coinbase-php api身份验证错误签名无效

coinbase-php api身份验证错误签名无效
EN

Stack Overflow用户
提问于 2020-06-25 04:03:40
回答 1查看 206关注 0票数 1

当我发送get请求来检索最新的地址时,它工作得很好,但是当我试图发送post请求来生成新地址时,我得到了这个错误,身份验证错误,无效签名

我确信一切都是正确的,因为我遵循了他们的文档,但我猜他们需要更新文档

代码语言:javascript
复制
$apiKey = "xx";
$apiSecret = "xxx"; 

$accountId = "accountx";      

$body = '';
$timestamp = time();
$message = $timestamp . 'GET' . '/v2/user' . $body;
$signature = hash_hmac('SHA256', $message, $apiSecret);
$version = '2020-06-23';

$headers = array(
    'CB-ACCESS-SIGN: ' . $signature,
    'CB-ACCESS-TIMESTAMP: ' . $timestamp,
    'CB-ACCESS-KEY: ' . $apiKey,
    'CB-VERSION: ' . $version
); 

$api_url = 'https://api.coinbase.com/v2/user';

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
    
$data = curl_exec($ch);

curl_close($ch);

$ctx = stream_context_create(["http"=>["user_agent"=>"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:63.0) Gecko/20100101 Firefox/63.0"]]);

$response_btc = file_get_contents('https://api.coinbase.com/v2/prices/BTC-USD/spot', true, $ctx);

$object_btc = json_decode($response_btc, true);

$usdprice = $object_btc["data"]["amount"];
$btcamount = number_format($amount/$usdprice, 8, '.', '');                    



$body1 = '';
$timestamp1 = time();
$message1 = $timestamp1 . 'POST' . '/v2/accounts/accountx/addresses' . $body1;
$signature1 = hash_hmac('SHA256', $message1, $apiSecret);
$version1 = '2020-06-23';

$headers1 = array(
    'CB-ACCESS-SIGN: ' . $signature1,
    'CB-ACCESS-TIMESTAMP: ' . $timestamp1,
    'CB-ACCESS-KEY: ' . $apiKey,
    'CB-VERSION: ' . $version1,
    'Content-Type: appliaction/json'
); 

$api_url = "https://api.coinbase.com/v2/accounts/accountx/addresses";

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $api_url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"name\": \"New receive address\"}");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                         


  
$data1 = curl_exec($ch);

if(curl_errno($ch))
{
    echo "Errore: " . curl_error($ch);
}
else
{
    echo $data1;
}

curl_close($ch);
EN

回答 1

Stack Overflow用户

发布于 2020-08-10 16:02:17

我也遇到了同样的错误,我用以下命令解决了这个问题:

代码语言:javascript
复制
json_encode($body);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62563194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档