如何为CoinBase API调用声明CURL主体 [英] How to declare CURL body for CoinBase API call
问题描述
我目前正在使用Coinbase的API开发小型应用程序。
I am currently working on a small app using the API of Coinbase.
Coinbase需要CB-ACCESS-SIGN标头进行身份验证。 CB-ACCESS-SIGN标头是通过使用预哈希字符串时间戳+方法+ requestPath +正文(其中+表示字符串串联)上的密钥创建sha256 HMAC生成的。
Coinbase needed CB-ACCESS-SIGN header to authenticate. The CB-ACCESS-SIGN header is generated by creating a sha256 HMAC using the secret key on the prehash string timestamp + method + requestPath + body (where + represents string concatenation).
参考页面 https://developers.coinbase.com/api/v2? shell#api-key
创建地址,基于以下地址: https://developers.coinbase.com/api/v2?shell#create-address 。我写了命令:
to create address, based from: https://developers.coinbase.com/api/v2?shell#create-address. I wrote command :
$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的请求正文
I think the problem is the request body at CB-ACCESS-SIGN
正文(其中+表示字符串连接)。
body (where + represents string concatenation).
正文值在哪里?
推荐答案
创建签名,如下所示:
$Datas = $timestamp.$method.$request_path;
$hmacSig = base64_encode(hash_hmac("sha256", $Datas, base64_decode($apiSecret), true));
这篇关于如何为CoinBase API调用声明CURL主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!