Bitcoin Forum
May 14, 2024, 01:58:24 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Authentication failed with the Coinbase API  (Read 152 times)
w_t (OP)
Newbie
*
Offline Offline

Activity: 28
Merit: 0


View Profile
March 31, 2018, 06:19:44 AM
Last edit: March 31, 2018, 06:32:47 AM by w_t
 #1

Goodmorning everyone,
I have an authentication problem with my account, through the Coinbase.com API.

I tell you what's happening to me:

- I opened a private account on Coinbase.com,
- I created my API Keys,
- I have enabled all the permissions to the keys, this means that I can perform all the operations possible with those APIs.
- I read the developer's guide and I tried to write a small script to test the authentication, but nothing completely useless ..., I return this error:

Code:
string(237) "{"errors":[{"id":"authentication_error","message":"invalid signature"}],"warnings":[{"id":"missing_version","message":"Please supply API version (YYYY-MM-DD) as CB-VERSION header","url":"https://developers.coinbase.com/api#versioning"}]}"

Since the examples are in other languages ​​but I use PHP, I think I have made a mistake during the conversion of the script.

This is the link of the official guide:

https://developers.coinbase.com/docs/wallet/api-key-authentication

This is my script:

Code:
<?
$API_KEY = '<---MY API KEY--->';
$API_SECRET = '<---MY API KEY SECRET--->';

$body = '';
$timestamp = time();
$message = $timestamp . 'GET' . 'https://www.mysite.it' . $body;
$signature = hash_hmac('SHA256', $message, $API_SECRET);

$headers = array(
'CB-ACCESS-SIGN: '.$signature,
'CB-ACCESS-TIMESTAMP: '.$timestamp,
'CB-ACCESS-KEY: '.$API_KEY
);

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


if(curl_errno($ch))
{
    echo "Errore: " . curl_error($ch);
}
else
{
    var_dump($data);
    curl_close($ch);
}
?>

Do you give me your opinion?
I've been stuck here for 5 days and I can not understand what I'm missing.
The assistance of Coinbase does not answer me and I have a project to deliver blocked.

Thank you very much for any useful advice.
1715651904
Hero Member
*
Offline Offline

Posts: 1715651904

View Profile Personal Message (Offline)

Ignore
1715651904
Reply with quote  #2

1715651904
Report to moderator
1715651904
Hero Member
*
Offline Offline

Posts: 1715651904

View Profile Personal Message (Offline)

Ignore
1715651904
Reply with quote  #2

1715651904
Report to moderator
1715651904
Hero Member
*
Offline Offline

Posts: 1715651904

View Profile Personal Message (Offline)

Ignore
1715651904
Reply with quote  #2

1715651904
Report to moderator
"Bitcoin: the cutting edge of begging technology." -- Giraffe.BTC
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
starmyc
Full Member
***
Offline Offline

Activity: 198
Merit: 130

Some random software engineer


View Profile
March 31, 2018, 08:06:47 AM
Last edit: March 31, 2018, 04:25:53 PM by starmyc
 #2

Code:
$message = $timestamp . 'GET' . 'https://www.mysite.it' . $body;
$signature = hash_hmac('SHA256', $message, $API_SECRET);

I did only take a quick look, but this seems wrong to me. According to the documentation, the "message" string must contain the path + params of the request url (eg: "/v2/exchange-rates?currency=USD"), not the complete url. If your complete url is "http://www.mysite.it/", then the request path you need to use is only "/". If you are calling https://api.coinbase.com/v2/user then you need to use '/v2/user' in $message.

Hi, I'm just some random software engineer.
You can check my projects: Bitcoin & altcoin balances/addresses listing dumps: https://balances.crypto-nerdz.org/
w_t (OP)
Newbie
*
Offline Offline

Activity: 28
Merit: 0


View Profile
March 31, 2018, 10:13:33 AM
 #3

Code:
$message = $timestamp . 'GET' . 'https://www.mysite.it' . $body;
$signature = hash_hmac('SHA256', $message, $API_SECRET);

I did only take a quick look, but this seems wrong to me. According to the documentation, the "message" string must contain the path + params (eg: "/v2/exchange-rates?currency=USD"), not the complete url. If your complete url is "http://www.mysite.it/", then the request path you need to use is only "/".

Excuse me if I do not understand,

my full URL what do you mean?
The redirect address that I put during the registration of my account?
starmyc
Full Member
***
Offline Offline

Activity: 198
Merit: 130

Some random software engineer


View Profile
March 31, 2018, 04:31:08 PM
 #4

I fixed my answer after testing. You can't use your website url in $message, you have to use the requestPath of the url you're querying. If you're querying "https://api.coinbase.com/v2/user", then you've to use "/v2/user" in $message to create the signature.

Please note it is recommended to use the CB-VERSION header as well.

The following code works:

Code:
<?php

$API_KEY 
'x';
$API_SECRET 'x';

$body '';
$timestamp time();
$message $timestamp 'GET' '/v2/user' $body;
$signature hash_hmac('SHA256'$message$API_SECRET);
$version '2017-11-11';

$headers = array(
                    
'CB-ACCESS-SIGN: ' $signature,
                    
'CB-ACCESS-TIMESTAMP: ' $timestamp,
                    
'CB-ACCESS-KEY: ' $API_KEY,
                    
'CB-VERSION: ' $version
                
); 

$api_url 'https://api.coinbase.com/v2/user';

$ch curl_init(); 
curl_setopt($chCURLOPT_URL$api_url);
curl_setopt($chCURLOPT_HTTPHEADER$headers);
curl_setopt($chCURLOPT_CUSTOMREQUEST"GET");
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_POST1);
    
$data curl_exec($ch);

if(
curl_errno($ch))
{
    echo 
"Errore: " curl_error($ch);
}
else
{
    echo 
$data;
}
curl_close($ch);
?>


Hi, I'm just some random software engineer.
You can check my projects: Bitcoin & altcoin balances/addresses listing dumps: https://balances.crypto-nerdz.org/
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!