Reputation: 11
When I send get request to retrieve the latest address it works fine but when I tried to send a post request to generate a new address I get this error authentication error invalid signature
I'm sure everything is correct because I followed their doc but I guess they need to update it
$apiKey = "xx";
$apiSecret = "xxx";
$accountId = "accountx";
$body = '';
$timestamp = time();
$message = $timestamp . 'GET' . '/v2/user' . $body;
$signature = hash_hmac('SHA256', $message, $apiSecret);
$version = '2020-06-23';
$headers = array(
'CB-ACCESS-SIGN: ' . $signature,
'CB-ACCESS-TIMESTAMP: ' . $timestamp,
'CB-ACCESS-KEY: ' . $apiKey,
'CB-VERSION: ' . $version
);
$api_url = 'https://api.coinbase.com/v2/user';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
$data = curl_exec($ch);
curl_close($ch);
$ctx = stream_context_create(["http"=>["user_agent"=>"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:63.0) Gecko/20100101 Firefox/63.0"]]);
$response_btc = file_get_contents('https://api.coinbase.com/v2/prices/BTC-USD/spot', true, $ctx);
$object_btc = json_decode($response_btc, true);
$usdprice = $object_btc["data"]["amount"];
$btcamount = number_format($amount/$usdprice, 8, '.', '');
$body1 = '';
$timestamp1 = time();
$message1 = $timestamp1 . 'POST' . '/v2/accounts/accountx/addresses' . $body1;
$signature1 = hash_hmac('SHA256', $message1, $apiSecret);
$version1 = '2020-06-23';
$headers1 = array(
'CB-ACCESS-SIGN: ' . $signature1,
'CB-ACCESS-TIMESTAMP: ' . $timestamp1,
'CB-ACCESS-KEY: ' . $apiKey,
'CB-VERSION: ' . $version1,
'Content-Type: appliaction/json'
);
$api_url = "https://api.coinbase.com/v2/accounts/accountx/addresses";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"name\": \"New receive address\"}");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$data1 = curl_exec($ch);
if(curl_errno($ch))
{
echo "Errore: " . curl_error($ch);
}
else
{
echo $data1;
}
curl_close($ch);
Upvotes: 1
Views: 302
Reputation: 83
I was getting the same error, and I solved it using:
json_encode($body);
Upvotes: 0