Swift 3,在Poloniex交易中Api得到 - "错误":无效的命令

时间:2016-09-05 04:52:34

标签: swift alamofire poloniex

我的代码:

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的任何信息... 所以,如果有人可以提供帮助 - 我将非常感激

2 个答案:

答案 0 :(得分:2)

以下是如何为NSURLRequest形成poloniex.com的示例。

想象一下你的:

  1. API Key = @“apikey”
  2. Secret = @“secret”
  3. nonce = @“1”
  4. 从最简单的事情开始:

    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(在我们的示例中只是@“秘密”)。

    以下功能会返回HMAC SHA512 NSData

    #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
    

    所以,基本上,只需从字符串中删除<>符号;

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

    您的theURLRequest现已准备好,应该成功获得tradingApi poloniex.com

答案 1 :(得分:0)

实际上它既不是Swift也不是iOS问题。 这是因为您正在访问Trading API方法,并且在POST请求中可能还需要更多其他参数(nonce除外):

检查一下:

  

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

     

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

因此:

  

交易API的所有回复都是JSON格式。在的情况下   错误,响应将始终采用以下格式:

     

{ “错误”: “”}

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

相关问题