我正在尝试在 Unity C# 中访问私有 Poloniex 交易 API,但收到错误“无效命令”我在 Poloniex 上为交易 API 授权了我的 API 密钥和秘密,但似乎无法使用我当前的代码访问:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Security.Cryptography;
public class PolonScript : MonoBehaviour {
public TextMesh OutputText;
const string _apiKey = "---Key---";
const string _apiSecret = "---secret---";
void Start () {
string nonce = DateTime.Now.ToString("HHmmss");
string myParam = "command=returnBalances&nonce=" + nonce;
const string WEBSERVICE_URL = "https://poloniex.com/tradingApi";
try
{
var webRequest = System.Net.WebRequest.Create(WEBSERVICE_URL);
if (webRequest != null)
{
webRequest.Method = "POST";
webRequest.Timeout = 12000;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Headers.Add("Key", _apiKey);
webRequest.Headers.Add("Sign", genHMAC(myParam));
webRequest.Headers.Add("command", "returnBalances");
webRequest.Headers.Add("nonce", nonce.ToString());
using (System.IO.Stream s = webRequest.GetResponse().GetResponseStream())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(s))
{
var jsonResponse = sr.ReadToEnd();
OutputText.text = jsonResponse.ToString();
}
}
}
}
catch (Exception ex)
{
OutputText.text = ex.ToString();
}
} //end-of-start()
这是我目前的签名方法,我很确定其中有一个错误(人为错误),我在这里不经意地做错了什么吗?
private string genHMAC(string message)
{
byte [] APISecret_Bytes = System.Text.Encoding.UTF8.GetBytes(_apiSecret);
byte [] MESSAGE_Bytes = System.Text.Encoding.UTF8.GetBytes(message);
var hmac = new HMACSHA512(APISecret_Bytes);
var hashmessage = hmac.ComputeHash(MESSAGE_Bytes);
var sign = BitConverter.ToString(hashmessage).Replace("-", "").ToLower();
return sign;
}
}