使用 HTTR 和 R 访问 Coinbase API

Access Coinbase API using HTTR and R

我正在尝试使用 API 将 Coinbase 帐户列表拉入 R。我收到一个身份验证错误消息 "invalid signature"。当我创建我的 sha256 签名时显然有问题,但我无法弄清楚问题是什么。使用 sha256 签名访问 GDAX API 时,我没有遇到同样的问题。

API 关键文档

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.

我的代码

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

抱歉没有早点更新。答案很简单,我只需要在指定请求路径时删除“/v2/accounts/”中最后的正斜杠即可。