【问题标题】:Trouble authenticating in Coinbase with PHP using PHP CURL使用 PHP CURL 在 Coinbase 中使用 PHP 进行身份验证时遇到问题
【发布时间】:2021-01-17 22:01:10
【问题描述】:

我正在尝试使用以下 PHP 代码对 Coinbase 进行身份验证,但我不断收到无效签名错误消息。

鉴于 info() 是一个函数(最终会将其变成一个对象),它会获取 API_KEY、API_SECRET、USERAGENT 和 Coinbase Base URL(每个都经过测试),并且 get_coinbase_time() 已经过测试以获取来自 Coinbase 的新纪元,我没有感到快乐。我的猜测是错误在 $sign 定义的那一行。文档对于如何解决这个问题不是很清楚。有人可以评估我的代码并推荐更改或提供我可以从中学习的可行代码吗?

谢谢!

<?php

var_dump(get_coinbase_access('/v2/accounts'));

function get_coinbase_access($path){   
    $data = get_coinbase_time() . 'GET' . $path;
    $sign = hash_hmac("sha256", $data, info('secret'));
    $headers = array();
    $headers[] = 'CB-ACCESS-KEY: ' . info('key');
    $headers[] = 'CB-ACCESS-SIGN: ' . $sign;
    $headers[] = 'CB-ACCESS-TIMESTAMP: ' . get_coinbase_time();
    $headers[] = 'CB-VERSION: 2016-03-08';
    $headers[] = 'Content-Type: application/json';


        $ch= curl_init(info('url') . $path);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT, info('useragent'));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        $response = curl_exec($ch);
        $res = json_decode($response, TRUE);
        return $res;
}
?>

【问题讨论】:

    标签: php coinbase-api coinbase-php


    【解决方案1】:

    我不是专家(我只是努力解决这个问题......)

    如果您关心 Coinbase 而不是 CoinbasePro,那么我认为 [the docs that you need][1] 部分说:

    CB-ACCESS-SIGN 标头是通过使用 prehash 字符串时间戳 + 方法 + requestPath + 正文(其中 + 表示字符串连接)上的密钥创建 sha256 HMAC 来生成的。时间戳值与 CB-ACCESS-TIMESTAMP 标头相同。

    所以,你的东西顺序不对。

    我想你可能会使用这样的东西:

    function signature($request_path='', $body='', $timestamp, $method='GET') {
      $w= $timestamp.$method.$request_path.$body;
      return hash_hmac("sha256", $w, API_SECRET, false);}
    

    【讨论】:

      【解决方案2】:

      试试这个:

        $timestamp = time();
        $body = '';
        $message = $timestamp . 'GET' . $path . $body;
        $sign = hash("sha256", COINBASE_SECRET_KEY . $message);
       
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-26
        • 2019-02-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-21
        • 1970-01-01
        • 2021-03-03
        相关资源
        最近更新 更多