无效的签名 Coinbase

Invalid Signature Coinbase

我正在尝试为 Coinbase 使用 API,但我得到无效签名。

所以可能我真的签错了或者我遗漏了什么。

我应该根据要求使用什么?我应该在方法上使用 POST 还是 GET?

$urlapi = "https://api.coinbase.com/v2/time";
            $Key = "--------------";
            $Secret = "------------";               
            $fecha = new DateTime();
            $timestamp = $fecha->getTimestamp();
            $request="";
            $body="";
            $method="GET";  
            $Datas = $timestamp . $method . $request . $body;               
            $hmacSig = hash_hmac('sha256',$Datas,$Secret);
            $curl = curl_init($urlapi);
            curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type: application/json','CB-ACCESS-KEY: '.$Key,'CB-VERSION: 2015-07-07','CB-ACCESS-TIMESTAMP: '. $timestamp,'CB-ACCESS-SIGN: '.$hmacSig));
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            $resp = curl_exec($curl);
            if(!curl_exec($curl)){
            die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
            }
            curl_close($curl);

            print_r($resp);

获取当前时间是一个 GET 请求 (ref)。 HTTP 动词(GET/POST 等)可以在此处每种请求类型的文档中找到:

在您的示例中,问题是消息中的 $request 变量为空。应该是$request="/v2/time";

hash_hmac returns 默认为十六进制编码字符串,因此散列部分是正确的。

define('API_KEY', 'xxxx');
define('API_SECRET', 'xxxxxxxxx');
define('API_BASE', 'https://api.coinbase.com');
define('API_VERSION', '2015-08-31');

$headers = array(
      'Content-Type: application/json',
      'CB-VERSION: ' . API_VERSION
    );

$url = API_BASE.'/v2/time';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
$output = json_decode($response, true);
print_r($output);

将return

Array ( [data] => Array ( [iso] => 2015-12-01T10:35:58Z [epoch] => 1448966158 ) )

必须使用GET方法,因为只是获取一些信息,不需要认证。

这是一种更简单的方法:

$time = file_get_contents('https://api.coinbase.com/v2/time');
$time = json_decode($time);
$time = $time->data->epoch;