Search code examples
rresthttrcoinbase-api

Access Coinbase API using HTTR and R


I am trying to pull a list of Coinbase accounts into R using their API. I receive an authentication error saying "invalid signature". Something is clearly wrong when I create my sha256 signature but I can't figure out what the issue is. I have not had this same issue when accessing the GDAX API using a sha256 signature.

API Key Documentation

API key is recommend if you only need to access your own account. All API >key requests must be signed and contain the following headers:

CB-ACCESS-KEY The api key as a string CB-ACCESS-SIGN The user generated message signature (see below) CB-ACCESS-TIMESTAMP A timestamp for your request All request bodies should have content type application/json and be valid JSON.

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). The timestamp value is the same as the CB-ACCESS-TIMESTAMP header.

My Code

library(httr)
library(RCurl)
library(digest)

coinbase_url <- "https://api.coinbase.com"
coinbase_reqPath <- "/v2/accounts/"
coinbase_fullPath <- paste(coinbase_url, coinbase_reqPath,sep = "")

coinbase_key <- "XXXXMYKEYXXX"
coinbase_secret <- "XXXXXMYSECRETKEYXXXX"

cb_timestamp <- format(as.numeric(Sys.time()), digits=10)
coinbase_message <- paste0(cb_timestamp,"GET", coinbase_reqPath)
coinbase_sig <- hmac(key = coinbase_secret, object = coinbase_message, algo = "sha256", raw = F)

coinbase_acct <- content(GET(coinbase_fullPath,
                        add_headers(
                        "CB-ACCESS-KEY" = coinbase_key, 
                        "CB-ACCESS-SIGN" = coinbase_sig,
                        "CB-ACCESS-TIMESTAMP" = cb_timestamp,
                        "Content-Type"="application/json")))

Solution

  • Sorry for not updating this earlier. The answer is simple, I just needed to remove the final forward slash in "/v2/accounts/" when specifying my request path.