无效的签名Coinbase

时间:2015-07-27 14:21:38

标签: coinbase-api

我尝试将API用于Coinbase,但我的签名无效。

所以我可能真的签了字或者我错过了什么。

我应该根据要求使用什么?我应该在方法上使用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);

3 个答案:

答案 0 :(得分:3)

Get Current Time is a GET request (ref). The HTTP verb (GET/POST etc.) can be found in the docs for each type of request here:

enter image description here

In your example, the problem is that the $request variable in your message is blank. It should be $request="/v2/time";

hash_hmac returns hex encoded string by default so the hashing part is correct.

答案 1 :(得分:1)

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);

将返回

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

答案 2 :(得分:1)

您必须使用GET方法,因为仅仅是为了获取一些信息,因此不需要身份验证。

这是一种更简单的方法:

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