powershell - Coinbase API - 无法发布
问题描述
这有什么问题?为什么我不能发布购买?我不断收到 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
解决方案
我之前错过了,你没有在你的哈希中包括身体。当您签名时,您需要包括正文选项。
$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");
推荐阅读
- flutter - Flutter 驱动程序:未找到测试文件
- php - 为什么 VS Code 删除结束标签?> 保存。有谁知道?
- java - 将任意对象传递给 Log4j 中的 ThreadContext.put()
- unity3d - Blender 到 Unity 骨架动画变形
- python - 如何在 AWS Lambda 中使用 Python 自定义包
- apache-poi - Apache POI SXSSFWorkbook createSheet() 返回 NullPointerException
- pentaho - lookup_Pentaho 数据集成
- firebase - firebase 云功能 webhook 返回后更新应用程序
- angular - 为什么我的 Angular 项目没有在 firestore 中写入数据?
- excel - 多个单元格或范围的 VBA Vlookup