2

我正在尝试在 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;
        }




    }
4

1 回答 1

1

Poloniex 命令不应在标头中发送,您应该将其作为 POST 参数发送,这就是它响应“无效命令”的原因。看看这个答案,看看你如何在 c# 中发送 POST 参数:如何将参数添加到 WebRequest?

下面是您的 Start 方法的示例:

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));

            byte[] dataStream = Encoding.UTF8.GetBytes($"command=returnBalances&nonce={nonce.ToString()}");
            webRequest.ContentLength = dataStream.Length;

            Stream newStream = webRequest.GetRequestStream();
            newStream.Write(dataStream, 0, dataStream.Length);
            newStream.Close();

            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();
    }
}
于 2018-04-13T16:56:51.970 回答