【问题标题】:How to declare CURL body for CoinBase API call如何为 CoinBase API 调用声明 CURL 主体
【发布时间】:2017-07-05 01:10:45
【问题描述】:

我目前正在使用 Coinbase 的 API 开发一个小应用程序。

Coinbase 需要 CB-ACCESS-SIGN 标头进行身份验证。 CB-ACCESS-SIGN 标头是通过使用 prehash 字符串时间戳 + 方法 + requestPath + 正文(其中 + 表示字符串连接)上的密钥创建 sha256 HMAC 来生成的。

参考页面https://developers.coinbase.com/api/v2?shell#api-key

创建地址,基于:https://developers.coinbase.com/api/v2?shell#create-address。我写了命令:

    $timestamp = time();
    $method = 'POST';
    $request_path = '/v2/accounts';
    $body = 'addresses';

    $account_id = 'myaaccount_id';
    $hash_input = $timestamp.''.$method.''.$request_path.''.$body;
    $apiSecret = 'myapi secret';
    $signature = hash_hmac('sha256', $hash_input, $apiSecret);
    $accesskey = 'myaccess_key';

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'https://api.coinbase.com/v2/accounts/'.$account_id.'/addresses');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');


    $headers = array();
    $headers[] = 'Cb-Access-Key: '.$accesskey;
    $headers[] = 'Cb-Access-Sign: '.$signature;
    $headers[] = 'Cb-Access-Timestamp: '.$timestamp;
    $headers[] = 'Cb-version: 2016-12-07';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);

但我总是得到回应:

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

我认为问题在于 CB-ACCESS-SIGN 的请求正文

body(其中 + 表示字符串连接)。

身体价值在哪里?

【问题讨论】:

    标签: php curl httprequest coinbase-api


    【解决方案1】:

    像这样创建签名:

    $Datas = $timestamp.$method.$request_path;
    $hmacSig = base64_encode(hash_hmac("sha256", $Datas, base64_decode($apiSecret), true));
    

    【讨论】:

      【解决方案2】:

      改变创建签名的方式

      $hash_input = $timestamp.$method.$request_path;
      $signature  = hash_hmac("sha256", $hash_input, $apiSecret);
      

      希望有帮助

      【讨论】:

      • 还是不行,伙计,我的签名无效。我想我写错了签名:时间戳+方法+请求路径+正文(其中+代表字符串连接)。而且我不知道“body”值是什么意思
      猜你喜欢
      • 1970-01-01
      • 2020-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 2019-11-01
      • 1970-01-01
      • 2020-07-19
      相关资源
      最近更新 更多