Coinbase API与PowerShell无效签名



我想通过powerShell的Coinbase API检索帐户余额。

我从Coinbase API文档中编码了以下读取,但最后一个请求列出以下错误:

Invoke-RestMethod : {"errors":[{"id":"authentication_error","message":"invalid signature"}]}

这是我的代码。怎么了?谢谢。

$accounts = 'https://api.coinbase.com/v2/accounts'
$time = 'https://api.coinbase.com/v2/time'
$epochtime = [string]((Invoke-WebRequest $time | ConvertFrom-Json).data).epoch
$method = 'GET'
$requestpath = '/v2/accounts'
$secret_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
$sign = $epochtime + $method + $requestpath
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Convert]::FromBase64String($secret_key)
$signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($sign))
$signature = [Convert]::ToBase64String($signature)
$header = @{
"CB-ACCESS-SIGN"=$signature
"CB-ACCESS-TIMESTAMP"=$epochtime
"CB-VERSION" = '2017-08-07'
"CB-ACCESS-KEY"='xxxxxxxxxxxxxx'
}
Invoke-WebRequest $accounts -Headers $header

希望这会让您前进。我今天刚刚开始在模块上工作,并陷入了同一件事。我在尝试解决问题时遇到了您的问题。我想分享我发现的东西。祝你好运!

$accounts = 'https://api.coinbase.com/v2/accounts'
$time = 'https://api.coinbase.com/v2/time'
$epochtime = [string]((Invoke-WebRequest $time | ConvertFrom-Json).data).epoch
$method = 'GET'
$requestpath = '/v2/accounts'
$secret_key = (Get-CoinBaseAPIKeys).Secret
$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 = ""
foreach ( $c in $computeSha )
{
    $signature += "{0:x2}" -f $c 
}

短途。奇怪的是,我在同一问题上陷入了困境,因为很短产生上案例十六进制,而^^上面的漫长的路转换为下部案例十六进制。Coinbase API仅在较低的情况下仅接受十六进制中的签名。

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

现在我们已经弄清楚了签名,其余的应该很好。我删除了CB_Version,因为它将默认为您自己的API版本。我的默认值不同,所以我只是将其删除。

$header = @{
"CB-ACCESS-SIGN"=$signature
"CB-ACCESS-TIMESTAMP"=$epochtime
"CB-ACCESS-KEY"=(Get-CoinBaseAPIKeys).Key
}
$result = Invoke-WebRequest $accounts -Headers $header -Method Get -ContentType "application/json"
$accounts = $result.Content | ConvertFrom-Json
Write-Output $accounts.data

作为存储私钥/秘密的旁边,您可以在这里找到一些想法:https://github.com/cmaahs/bittrexapi/tree/master/encryption。随意抓住它并按照自己的方式进行修改。最好将密钥/秘密存储在注册表中,而不是脚本或环境变量中的纯文本。

最新更新