【问题标题】:Coinbase API - Cannot POSTCoinbase API - 无法发布
【发布时间】:2021-10-26 08:05:02
【问题描述】:

这有什么问题?为什么我不能发布购买?我不断收到 401 Unauthorized。 API 有正确的权限(wallet:buys:create)

我应该指出,我的 GET 有效,我可以从帐户中读取所有信息。

$time = 'https://api.coinbase.com/v2/time'
$epochtime = [string]((Invoke-WebRequest $time | ConvertFrom-Json).data).epoch

$method = 'POST'
$requestpath = '/v2/accounts/xxxxxxxx-3ecb-xxxxxxxx-xxxxxxxx/buys'
$endpoint = "https://api.coinbase.com/$($requestpath)"
$secret_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

$sign = $epochtime + $method + $requestpath
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Text.Encoding]::UTF8.GetBytes($secret_key)
$computeSha = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($sign))

$signature = ([System.BitConverter]::ToString($computeSha) -replace "-").ToLower()

$header = @{
"CB-ACCESS-SIGN"=$signature
"CB-ACCESS-TIMESTAMP"=$epochtime
"CB-ACCESS-KEY"='xxxxxxxxxxxxxxxxxxxx'
}

$body = '{"amount": "10", "currency": "XLM", "payment_method": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "commit": "true", "quote":"false"}'
function Get-CoinBase($method, $endpoint, $header, $body)
{
  $result = Invoke-WebRequest $endpoint -Headers $header -Method $method -body $body -ContentType "application/json" -UseBasicParsing
  write-host $APImethod -f yellow

  return $result
}

$AccountBAL = Get-CoinBase -method "POST" -endpoint $endpoint -header $header -body $body

【问题讨论】:

  • 为什么不使用exchange api而不是coinbase api?
  • 您的消息中的错误是什么?无效的签名/密钥/时间戳?
  • 我也将只使用 CoinBasePro API,但我真的只想为非专业 Coinbase API 解决这个问题。返回的错误是“无效签名”,但据我所知,我正确地形成了签名?我所有的GETS都可以使用相同的$requestpath变量正常工作,该变量构成sig的一部分。
  • 看看这篇文章。似乎 cb 和 cbpro 的签名可能略有不同stackoverflow.com/questions/69714766/…
  • 你可能会在这里做点什么!时间服务器比我的电脑晚 1 小时。我在爱尔兰,夏令时将在周日开始(我们回滚 1 小时)。如果这是问题所在,而且周一才开始工作,我会被难住的!

标签: powershell coinbase-api


【解决方案1】:

我之前错过了,您没有在哈希中包含正文。签名时,您需要包含正文选项。

$sign = $epochtime + $method + $requestpath

应该是

$sign = $epochtime + $method + $requestpath + $body

这是他们的例子

var message = timestamp + req.method + req.path + req.body;
//create a hexedecimal encoded SHA256 signature of the message
var signature = crypto.createHmac("sha256", apiSecret).update(message).digest("hex");

【讨论】:

  • 很好看。我错过了,但仍然收到“无效签名”错误。我将 $body 定义为哈希表,然后转换为 JSON,然后将其连接到签名。我假设它应该被连接为 JSON?
  • 我使用 node 并查看它正在运行的工作 npm 模块json.stringify(body) 看起来结果正是你的$body 变量在 OP 中所持有的(除非它不喜欢引号大约 10,因为它是一个数字)...developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 话虽如此,它似乎只需要提供的正文来确认您的签名。我希望他们的文档更清楚地了解签名。
  • 哦,伙计,你是救生员,谢谢
猜你喜欢
  • 1970-01-01
  • 2017-03-23
  • 1970-01-01
  • 2018-02-18
  • 1970-01-01
  • 2020-06-08
  • 2015-04-27
  • 1970-01-01
  • 2018-05-04
相关资源
最近更新 更多