【问题标题】:I am trying to Access the private Poloniex trading API in Unity C# but am getting the error "invalid command"我正在尝试在 Unity C# 中访问私有 Poloniex 交易 API,但收到错误“无效命令”
【发布时间】:2018-09-24 01:43:20
【问题描述】:

我正在尝试访问 Unity C# 中的私有 Poloniex 交易 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;
        }




    }

【问题讨论】:

    标签: c# private-key trading poloniex


    【解决方案1】:

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

    下面是您的 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();
        }
    }
    

    【讨论】:

    • 我看了看并从标题中删除了它,实际上我可以从标题中删除除 KEY 和 Secret 之外的所有内容,但我仍然得到“无效命令”。这是正常的吗?如果我使用我的密钥/秘密组合与随机字母似乎也没关系,所以我知道甚至在 HMAC 部分之前还有一些额外的错误
    • 这里是Web Request的内部://webRequest.Method = "POST"; //webRequest.Timeout = 12000; //webRequest.ContentType = "应用程序/x-www-form-urlencoded"; webRequest.Headers.Add(“密钥”,_apiKey); webRequest.Headers.Add ("签名", genHMAC (myParam)); //webRequest.Headers.Add ("command", "returnBalances"); //webRequest.Headers.Add("nonce", nonce.ToString());
    • 你需要发送命令,你不能只是评论它,我认为你得到的响应是正常的。尝试发送我在答案中指定的命令,看看它是否仍然返回相同的错误。如果您在编写代码时遇到任何问题,请告诉我,我会尽力提供帮助。
    • 你是在说你给我​​看的这部分吗?: if (request.Method == "POST") { if (postData != null) { request.ContentLength = postData.Length;使用 (var dataStream = request.GetRequestStream()) { dataStream.Write(postData, 0, postData.Length); } } }
    • 我已经编辑了答案以反映代码的外观,您可以尝试一下,看看它是否解决了“无效命令”问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 2022-08-02
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多