0

我在调用 MTGox HTTP api v2 时遇到问题。我写了一个 sendrequest 函数来处理我所有的请求。它适用于 MONEY/INFO 或 MONEY/ORDERS,但是当我尝试 MONEY/ORDER/QUOTE 或 MONEY/ORDER/ADD 方法时,我收到 500 个内部服务器错误。

似乎当 post_data 包含除了 nonce 之外的任何内容时,它会出错。我该怎么做才能解决这个问题?

发送请求函数:

private string sendRequest(string action, NameValueCollection query)
    {

        NameValueCollection nvc = new NameValueCollection();
        nvc.Add("nonce", DateTime.Now.Ticks.ToString());
        nvc.Add(query);

        String post_data = "";
        for (int i = 0; i < nvc.Count; i++)
        {
            post_data += "&";
            post_data += nvc.Keys[i];
            post_data += "=";
            post_data += nvc[i];
        }
        post_data = post_data.Substring(1);


        action = "BTCEUR/money/" + action;
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sBasePath + action);
        action += "\0"+post_data;
        req.Method = "POST";

        HMACSHA512 hmac = new HMACSHA512(GetBytes(action));
        hmac.Key = Convert.FromBase64String(secret);
        String sign = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(action)), Base64FormattingOptions.None);


        req.Headers.Add("Rest-Key", apikey);
        req.Headers.Add("Rest-Sign", sign);

        req.UserAgent = "Mozilla/4.0 (compatible; MtGoxTradeCLI)";
        req.ContentType = "application/x-www-form-urlencoded";

        StreamWriter reqStream = new StreamWriter(req.GetRequestStream());
        reqStream.Write(post_data);
        reqStream.Close();

        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

        StreamReader respStream = new StreamReader(resp.GetResponseStream());
        String response = respStream.ReadToEnd();
        respStream.Close();

        return response;
    }
4

1 回答 1

-1

对于任何需要参数的请求,请确保您签名中的 nonce 在您的签名中排在最后,例如对于货币 USD 的货币/钱包/历史记录,您的签名应该是:

money/wallet/history\0currency=USD&nonce=xxxxxxxxxx

(\0 是一个空字符,以防万一您想知道)

此外,MtGox 的 API 现在似乎在不断变化——例如,对于上面的钱包历史,我们曾经调用:

BTCUSD/money/wallet/history

作为 API 端点,但这似乎不再起作用。现在我们调用:

money/wallet/history

因此,如果曾经为您工作的呼叫现在失败了,也请看一下。但我可以肯定地告诉你,将你的 nonce 作为你签名的 QS 中的最后一个参数是至关重要的,否则你的 API 调用将不再有效。

我还建议在 MtGox 中重新创建一个新的 API 密钥——我们必须这样做才能使我们的代码也能正常工作。我有一种预感,这是因为 MtGox 最近的 API 更改已使旧密钥无效(可能会阻止交易机器人在他们弄清楚如何在不让机器人杀死他们的 API 的情况下处理它们时)

于 2013-04-24T02:29:00.627 回答