powershell Coinbase API -无法开机自检

mxg2im7a  于 2022-12-23  发布在  Shell
关注(0)|答案(1)|浏览(125)

这有什么问题?为什么我不能发布购买?我一直得到401未授权。API有正确的权限(钱包:购买:创建)
我应该指出,我的GET工作,我可以从帐户中读取所有信息。

  1. $time = 'https://api.coinbase.com/v2/time'
  2. $epochtime = [string]((Invoke-WebRequest $time | ConvertFrom-Json).data).epoch
  3. $method = 'POST'
  4. $requestpath = '/v2/accounts/xxxxxxxx-3ecb-xxxxxxxx-xxxxxxxx/buys'
  5. $endpoint = "https://api.coinbase.com/$($requestpath)"
  6. $secret_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  7. $sign = $epochtime + $method + $requestpath
  8. $hmacsha = New-Object System.Security.Cryptography.HMACSHA256
  9. $hmacsha.key = [Text.Encoding]::UTF8.GetBytes($secret_key)
  10. $computeSha = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($sign))
  11. $signature = ([System.BitConverter]::ToString($computeSha) -replace "-").ToLower()
  12. $header = @{
  13. "CB-ACCESS-SIGN"=$signature
  14. "CB-ACCESS-TIMESTAMP"=$epochtime
  15. "CB-ACCESS-KEY"='xxxxxxxxxxxxxxxxxxxx'
  16. }
  17. $body = '{"amount": "10", "currency": "XLM", "payment_method": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "commit": "true", "quote":"false"}'
  18. function Get-CoinBase($method, $endpoint, $header, $body)
  19. {
  20. $result = Invoke-WebRequest $endpoint -Headers $header -Method $method -body $body -ContentType "application/json" -UseBasicParsing
  21. write-host $APImethod -f yellow
  22. return $result
  23. }
  24. $AccountBAL = Get-CoinBase -method "POST" -endpoint $endpoint -header $header -body $body
nxowjjhe

nxowjjhe1#

我之前没注意到,你没把身体算进去。你签字的时候要把身体算进去。

  1. $sign = $epochtime + $method + $requestpath

应该是

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

下面是他们例子

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

相关问题