Bash curl 标志 hmac

nyi*_*guy 3 api rest bash curl

我正在尝试使用 Bittrex API。提供的唯一示例如下。我什至不确定这是什么语言。我正在尝试在 bash 中复制它。完整的 API 详细信息位于此处https://bittrex.com/Home/Api

$apikey='xxx';
$apisecret='xxx';
$nonce=time();
$uri='https://bittrex.com/api/v1.1/market/getopenorders?apikey='.$apikey.'&nonce='.$nonce;
$sign=hash_hmac('sha512',$uri,$apisecret);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
$execResult = curl_exec($ch);
$obj = json_decode($execResult);
Run Code Online (Sandbox Code Playgroud)

我尝试了几件事,但这是我尝试过的最新一件事。

#Bash
apikey="mykey"
secret="mysecret"
nonce=`date +%s`
uri="https://bittrex.com/api/v1.1/market/getopenorders?apikey=$apikey&nonce=$nonce"
apisig=`echo -n "$uri" | openssl dgst -sha512 -hmac "$secret"`

curl -sG https://bittrex.com/api/v1.1/market/getopenorders?nonce="$nonce"&apikey="$apikey"&apisig="$apisig"
Run Code Online (Sandbox Code Playgroud)

我得到 "{"success":false,"message":"APIKEY_NOT_PROVIDED","re​​sult":null}"

orh*_*ej2 5

你缺少的是:

  • &在查询字符串中转义
  • 将摘要作为标题而不是参数传递

所以对我有用的代码是:

#!/bin/bash

apikey="mykey"
secret="mysecret"
nonce=`date +%s`
uri="https://bittrex.com/api/v1.1/market/getopenorders?apikey=$apikey&nonce=$nonce"
apisig=`printf %s "$uri" | openssl dgst -sha512 -hmac "$secret"| sed 's/^.*= //'`

curl -sG $uri --header "apisign: $apisig"
Run Code Online (Sandbox Code Playgroud)