How to declare CURL body for CoinBase API call

こ雲淡風輕ζ 提交于 2019-12-08 07:04:39

问题


I am currently working on a small app using the API of Coinbase.

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

Reference page https://developers.coinbase.com/api/v2?shell#api-key

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

but I always got response :

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

I think the problem is the request body at CB-ACCESS-SIGN

body (where + represents string concatenation).

Where is body value?


回答1:


Change the way for create signature

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

Hope this help




回答2:


Create signature like this:

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


来源:https://stackoverflow.com/questions/42265223/how-to-declare-curl-body-for-coinbase-api-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!