【问题标题】:Api key authentication for coinbasecoinbase 的 API 密钥认证
【发布时间】:2017-10-26 20:00:45
【问题描述】:

我正在尝试为 API coinbase.com 编写请求,但无法正确生成签名。两天来我一直试图找出我的错误,但我做不到。我在页面上分析了其他语言的代码:https://developers.coinbase.com/docs/wallet/api-key-autumnicathion,但我没有看到任何实现差异。

请帮帮我。

<?php
$g_coinbase_key = 'KcxisxqmWRVgtwsj';
$g_coinbase_secret = 'isOLGBLaEkCy3ROQMvmjonGmXK0KRmUS';

$time = time();
$method = "GET";
$path = '/v2/accounts/';
$sign = base64_encode(hash_hmac("sha256", $time.$method.$path, $g_coinbase_secret));
$ch = curl_init('https://api.coinbase.com'.$path);
$headers = array(
    "CB-VERSION: 2017-10-26",
    "CB-ACCESS-SIGN: ".$sign,
    "CB-ACCESS-TIMESTAMP: ".$time,
    "CB-ACCESS-KEY: ".$g_coinbase_key,
    "Content-Type: application/json"
);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
var_dump($result);
?>

结果:

{"errors":[{"id":"authentication_error","message":"invalid signature"}]}

【问题讨论】:

    标签: signature sha256 coinbase-api coinbase-php


    【解决方案1】:

    像这样创建签名:

    $time = time();
    $method = "GET";
    $path = 'accounts';
    $sign = base64_encode(hash_hmac("sha256", $time.$method.$path, base64_decode($g_coinbase_secret), true));
    

    替换

    $ch = curl_init('https://api.coinbase.com'.$path);
    

    $ch = curl_init('https://api.coinbase.com/v2/');
    

    【讨论】:

    • 曾经得到这个工作?我正在尝试在 iOS 和 Swift 中做类似的事情,但似乎无法让签名正常工作。这怎么可能是正确的: $ch = curl_init('api.coinbase.com/v2/'); ??
    【解决方案2】:

    替换

    $sign = base64_encode(hash_hmac("sha256", $time.$method.$path, $g_coinbase_secret));
    

    $sign = hash_hmac("sha256", $time.$method.$path, $g_coinbase_secret);
    

    Coibase Api 使用 hash_mac

    【讨论】:

      【解决方案3】:

      要正确创建签名,Coinbase Pro 将接受使用其 API 文档中的以下代码:

      class CoinbaseExchange {
          public function __construct($key, $secret, $passphrase) {
              $this->key = $key;
              $this->secret = $secret;
              $this->passphrase = $passphrase;
          }
      
          public function signature($request_path='', $body='', $timestamp=false, $method='GET') {
              $body = is_array($body) ? json_encode($body) : $body;
              $timestamp = $timestamp ? $timestamp : time();
      
              $what = $timestamp.$method.$request_path.$body;
      
              return base64_encode(hash_hmac("sha256", $what, base64_decode($this->secret), true));
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-05
        • 1970-01-01
        • 1970-01-01
        • 2012-03-30
        • 1970-01-01
        • 2021-03-16
        • 2016-02-14
        相关资源
        最近更新 更多