【问题标题】:Swift 3, in Poloniex trade Api get - "error": Invalid commandSwift 3,在 Poloniex 交易 Api 中获取 - “错误”:无效命令
【发布时间】:2017-04-18 06:09:54
【问题描述】:

我的代码:

func testApi() {
        Alamofire.request("https://www.poloniex.com/tradingApi", withMethod: .post, parameters: ["command":"returnDepositAddresses","nonce":nonce()], encoding: .json, headers: ["Key":apiKey,"Sign":newSecret]).responseJSON() { (dataBack) in
            print(dataBack)
        }
    }
func nonce() -> Int {
        let date = "\(NSDate().timeIntervalSince1970)"
        let UnixInt = Double(date)!
        return Int(UnixInt)
    }

我明白了:

SUCCESS: {
error = "Invalid command.";}

我找不到任何有关使用 Swift 或 Objective C 的 poloniex api 的信息... 因此,如果有人可以提供帮助 - 我将非常感激

【问题讨论】:

  • 你确定 '["error": Invalid command.]' 来自那个确切的打印行吗?
  • @pedrouan 是的。我在写这个主题时删除了“打印”命令。但已经编辑过了。
  • 好吧。该错误与 API 无关,因为响应显示正确。请将此行附加在您的“打印(dataBack.response)”行之后:print(dataBack)
  • @pedrouan 好的,我添加它。 Optional(<NSHTTPURLResponse: 0x600000031020> { URL: https://www.poloniex.com/tradingApi } { status code: 200, headers { "Cache-Control" = private; "Content-Encoding" = gzip; "Content-Type" = "application/json"; Date = "Mon, 05 Sep 2016 05:11:34 GMT"; Server = "cloudflare-nginx"; "cf-ray" = "2dd72e64b2dc30e4-SIN"; } }) SUCCESS: { error = "Invalid command."; } SUCCESS
  • 我认为参数可能有问题...因为如果我将其更改为 ["command":"anything"] - 这将是同样的错误。

标签: swift alamofire poloniex


【解决方案1】:

下面是一个如何为poloniex.com 形成NSURLRequest 的示例。

想象一下你的:

  1. API Key = @"apikey"
  2. Secret = @“秘密”
  3. nonce = @"1"

从最简单的事情开始:

NSMutableURLRequest *theURLRequest = [NSMutableURLRequest new];
theURLRequest.URL = [NSURL URLWithString:@"https://poloniex.com/tradingApi"];
theURLRequest.HTTPMethod = @"POST";

NSString *theBodyString = @"command=returnBalances&nonce=1";

theURLRequest.HTTPBody = [theBodyString dataUsingEncoding:NSUTF8StringEncoding];
[theURLRequest setValue:@"apikey" forHTTPHeaderField:@"Key"];

现在是最难的一点......

就我而言,Poloniex 文档并不太清楚他们想要在 "Sign" 标头字段值下的内容,但基本上他们希望您传递一个字符串,这应该是 HMAC SHA512 加密算法应用于theBodyString Secret(在我们的示例中只是@“secret”)。

这是返回HMAC SHA512NSData的函数:

#import <CommonCrypto/CommonHMAC.h>
NSData * getHMACSHA512FromSecretKeyStringAndBodyString(NSString *theSecretKeyString, NSString *theBodyString)
{
    const char *cSecret  = [theSecretKeyString cStringUsingEncoding:NSUTF8StringEncoding];
    const char *cBody = [theBodyString cStringUsingEncoding:NSUTF8StringEncoding];
    unsigned char cHMAC[CC_SHA512_DIGEST_LENGTH];
    CCHmac(kCCHmacAlgSHA512, cSecret, strlen(cSecret), cBody, strlen(cBody), cHMAC);
    return [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
}

所以,运行:

NSData *theData = getHMACSHA512FromSecretKeyStringAndBodyString(@"secret", @"command=returnBalances&nonce=1");
NSString *theString = [NSString stringWithFormat:@"%@", theData];

会给我们几乎我们想要的东西。

我们的结果等于:

<c288f881 a6808d0e 78827ec6 ca9d6b9c 34ec1667 07716303 0d6d7abb 2b225456 31176f52 8347ab0f d6671ec5 3aec1f7d 3b6de8b8 e3ccc23d e62fd594 52d70db5>

而我们真正想要的(根据http://www.freeformatter.com/hmac-generator.html)是:

c288f881a6808d0e78827ec6ca9d6b9c34ec1667077163030d6d7abb2b22545631176f528347ab0fd6671ec53aec1f7d3b6de8b8e3ccc23de62fd59452d70db5

所以,基本上,只需从字符串中删除 &lt;&gt; 符号即可;

theString = [theString stringByReplacingOccurrencesOfString:@"<" withString:@""];
theString = [theString stringByReplacingOccurrencesOfString:@">" withString:@""];
theString = [theString stringByReplacingOccurrencesOfString:@" " withString:@""];
[theURLRequest setValue:theString forHTTPHeaderField:@"Sign"];

您的theURLRequest 现已准备就绪,应该可以成功获得poloniex.comtradingApi

【讨论】:

  • 这太棒了!你完全正确,这对我帮助很大。谢谢! :)
【解决方案2】:

其实这既不是 Swift 也不是 iOS 问题。 这是因为您正在访问 Trading API 方法,它们可能需要在您的 POST 请求中添加一些额外的参数(nonce 除外):

检查一下:

对交易 API 的所有调用都通过 HTTP POST 发送到 https://poloniex.com/tradingApi 并且必须包含以下内容 标题:

Key - 您的 API 密钥。签名 - 查询的 POST 数据由您的密钥签名 根据 HMAC-SHA512 方法“秘密”。此外,所有 查询必须包含“nonce”POST 参数。随机数参数是 一个整数,必须始终大于先前使用的随机数。

因此:

来自交易 API 的所有响应均采用 JSON 格式。在的情况下 错误,响应将始终采用以下格式:

{"错误":""}

https://temp.poloniex.com/support/api/

【讨论】:

  • 我已经添加了标题密钥和签名代码(当这个错误时 - 我得到“错误:无效的密钥或签名代码”)所以我无法理解什么命令无效
  • 我的意思是,钥匙和标志肯定是正确的。这是 API 或请求错误。我在这里的主题是解决这个错误。如果可能的话……
  • 好的,我没看懂你之前的评论,看来你的签名码有问题。你之前有没有通过任何先前的命令?或者这是你第一次跑步?试试这个set the "account" POST parameter to "all"
  • 现在键和符号是正确的。当我写错密钥时 - 我得到“error =”Invalid API key/secret pair。”现在我解决了这个问题。但第二个问题(我创建这个主题的原因) - 是“error = “Invalid command”。我想了解我现在必须做什么...我尝试使用许多不同的参数,但得到相同的错误。在 java 示例代码中,他们只使用“returnBalances”,没有任何额外的命令或参数...
  • 是的。但是如果我使用 "command":"returnDepositAddresses" - 我会得到同样的错误。如果我使用 "hello":"hello" - 我会得到同样的错误...对于任何参数我都会得到同样的错误。
猜你喜欢
  • 2018-09-24
  • 1970-01-01
  • 2017-11-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-21
  • 2017-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多