【问题标题】:Coinbase API invalid signature with Powershell带有 Powershell 的 Coinbase API 无效签名
【发布时间】:2017-12-07 22:41:56
【问题描述】:

我想通过 Coinbase API 和 Powershell 检索账户余额。

我从 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

【问题讨论】:

    标签: api powershell coinbase-api


    【解决方案1】:

    希望这能让你继续前进。我今天刚开始研究一个模块,并被困在同一件事上。我在尝试自己解决问题时遇到了您的问题。想我会分享我的发现。祝你好运!

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

    LONG WAY,供参考:

    $signature = ""
    foreach ( $c in $computeSha )
    {
        $signature += "{0:x2}" -f $c 
    }
    

    捷径。奇怪的是我陷入了同样的问题,因为捷径 产生大写十六进制,很长的路^^以上^^转换为小写十六进制。 CoinBase API 将只接受小写的十六进制签名。

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

    现在我们已经确定了签名,其余的应该可以正常工作。我删除了 CB_VERSION 因为它将默认为您的 OWN 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
    

    作为存储 PRIVATE KEY/SECRET 的旁白,您可以在这里找到一些想法: https://github.com/cmaahs/BittrexAPI/tree/master/Encryption。随意抓住它并随意修改。最好将加密的 KEY/SECRET 存储在注册表中,而不是作为脚本中的纯文本或环境变量。

    【讨论】:

    • 真的很棒!!!错过了 Coinbase 文档 API 中签名的小写 HEX...
    • 我不认为它是指定的,至少我没有看到,我很幸运,很长的路产生了一个小写版本并且可以工作。
    猜你喜欢
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 2019-01-12
    • 2015-04-21
    • 2019-04-07
    • 1970-01-01
    相关资源
    最近更新 更多