2016-09-05 8 views
0

マイコード:スウィフト3は、Poloniex貿易APIで取得 - "エラー":無効なコマンド

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.";} 

を私はpoloniex APIに関するすべての情報を見つけることができませんスウィフトまたは目的のCを使用して... 誰かが助けることができるなら - 私は非常に感謝します

+0

は、あなたは必ず「[ 『エラー』:無効なコマンド。]」は、正確な印刷ラインから来ていますか? – pedrouan

+0

@pedrouanはい。私はこのトピックを書くときに "print"コマンドを削除しました。しかし、すでにそれを編集する。 – VladyslavPG

+0

よく。応答は正しく表示されるため、エラーはAPIに関連していません。 'print(dataBack.response)'の直後にこの行を追加してください: 'print(dataBack)' – pedrouan

答えて

0

実際にそれはスウィフトでもIOS問題ではありません。 あなたが取引のAPIメソッドにアクセスしているので、それはだ、と彼らはあなたのPOSTリクエストで(ナンスのを除く)いくつかのより多くの追加のパラメータが必要な場合があります:

はこれを確認します。

All calls to the trading API are sent via HTTP POST to https://poloniex.com/tradingApi and must contain the following headers:

Key - Your API key. Sign - The query's POST data signed by your key's "secret" according to the HMAC-SHA512 method. Additionally, all queries must include a "nonce" POST parameter. The nonce parameter is an integer which must always be greater than the previous nonce used.

したがって:

All responses from the trading API are in JSON format. In the event of an error, the response will always be of the following format:

{"error":""}

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

+0

私はすでにキーヘッダを追加し、コードの署名(この間違ったのとき - 私は、「エラー:無効なキーまたは登録コード」を取得) だから私は確かに右であるキーや記号、私が意味する – VladyslavPG

+0

無効である何かコマンドを理解することはできません。 APIまたは要求エラーです。そして、このエラーを解決するための私のトピックはここにあります。彼の可能な場合... – VladyslavPG

+0

OK私はあなたの前のコメントを理解していない、それはあなたがサインコードに問題があるようだ。前のコマンドを前に渡しましたか?または、これが最初に実行したものですか?そして、これを試してみてください。 "アカウント" POSTパラメータを "all"に設定してください。 – pedrouan

1

NSURLRequestpoloniex.comに設定する方法の例を示します。

想像しているあなた:

  1. API Key = @ "APIキー"
  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暗号化アルゴリズムの結果がtheBodyStringSecret(この例では単に「秘密」です)の両方に適用されます。実行している、だから、

#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]; 

は、我々が望んでいたほとんど何私たちにを与えるだろう。ここ

はあなたにHMAC SHA512NSDataを返す関数です。

私たちの結果は以下に等しい。私たちが実際に(http://www.freeformatter.com/hmac-generator.htmlあたりのように)したいことですが

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

:だから

c288f881a6808d0e78827ec6ca9d6b9c34ec1667077163030d6d7abb2b22545631176f528347ab0fd6671ec53aec1f7d3b6de8b8e3ccc23de62fd59452d70db5 

、基本的には、ちょうどから<>記号を削除あなたのひも;

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

あなたtheURLRequestの準備が整いましたとpoloniex.comtradingApiを取得成功する必要があります。

+0

これは素晴らしいことです!あなたはそれを完璧に得て、これは私を助けました。ありがとう! :) –

関連する問題