我正在尝试在Unity C#中访问私有Poloniex交易API,但收到错误"invalid command"



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


    }

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

相关内容

最新更新