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 예. 이 주제를 쓸 때 "인쇄"명령을 제거했습니다. 그러나 이미 그것을 편집하십시오. – 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

이미 키 헤더를 추가하고 코드를 로그인합니다. API 또는 요청 오류입니다. 그리고 여기에 대한 내 주제는이 오류를 해결합니다. 그의 가능성이 있다면 ... – VladyslavPG

+0

내 말은 잘못된 키이며, 기호 확실히 옳은 일을 명령 이해할 수 없다 : - (I 얻을 "잘못된 키 또는 코드 서명 오류"때 잘못된) – VladyslavPG

+0

OK 나는 당신의 이전 의견을 이해하지 못했고, 당신은 사인 코드에 문제가 있다고 보았다. 전에 이전 명령을 통과 했습니까? 아니면 이번이 처음입니까? 그리고 이것을 시도하십시오' "계정"POST 매개 변수를 "all"로 설정하십시오. – pedrouan

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 암호화 알고리즘의 결과는 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 SHA512 NSData을 반환하는 기능입니다.

우리가 실제로 ( 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 지금 준비하고 tradingApipoloniex.com의를 받고 성공해야한다.

+0

대단하군요! 너는 그것을 완벽하게 받아 들였고 이것은 나에게 많은 도움이되었다. 감사! :) –