我正在尝试使用 HMAC/sha512 API 密钥方案进行身份验证。
这是示例 Python 代码,它工作正常:
import urllib, urllib2
import json
import time
import hmac, hashlib
import sys
api_key = "J88PJQEG-LKKICZLN-3H33GWIB-97OGW8I5"
secret = "b9f2e97c5c43e8e759c06219b37fce78478985ae4b0176d47182419c434567405a9386a854bca5d068135d1163d3f1cc9b877cd5d95d03c9d100be6ffcaac864"
# cmd = sys.argv[1]
# args = json.loads(sys.argv[2])
def auth_request(command, args):
args = [("command", command), ("nonce", 3000000000)]
post_data = urllib.urlencode(args)
print post_data
sign = hmac.new(secret, post_data, hashlib.sha512).hexdigest()
print sign
headers = {
'Sign': sign,
'Key': api_key
}
ret = urllib2.urlopen(urllib2.Request('https://poloniex.com/tradingApi', post_data, headers))
return ret.read()
print auth_request("returnBalances", {})
现在我的 Go 实现:
const (
public_api_url = "https://poloniex.com/public"
private_api_url = "https://poloniex.com/tradingApi"
pubkey := "J88PJQEG-LKKICZLN-3H33GWIB-97OGW8I5"
privkey := "b9f2e97c5c43e8e759c06219b37fce78478985ae4b0176d47182419c434567405a9386a854bca5d068135d1163d3f1cc9b877cd5d95d03c9d100be6ffcaac864"
)
func CallPrivate(method string, args map[string]string) dynjson.DynNode {
if args == nil {
args = make(map[string]string)
}
v := make(url.Values)
v.Set("nonce", "3000000000") //strconv.Itoa(int((time.Now().Unix()))*1000))
v.Set("command", method)
for k, val := range args {
v.Set(k, val)
}
final_url := private_api_url + "?" + v.Encode()
log.Println(final_url)
client := &http.Client{}
post_data := v.Encode()
secret_bytes, err := hex.DecodeString(privkey)
check(err)
sighash := hmac.New(sha512.New, secret_bytes)
sighash.Write([]byte(post_data))
sigstr := hex.EncodeToString(sighash.Sum(nil))
log.Println(sigstr)
j, err := json.Marshal(args)
现在我坚持调用 python 实现,这不是一个很好的解决方案。
相关分类