NSURLRequest
をpoloniex.com
に設定する方法の例を示します。
想像しているあなた:
API Key
= @ "APIキー"
Secret
= @ "秘密"
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
(この例では単に「秘密」です)の両方に適用されます。実行している、だから、
#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
の準備が整いましたとpoloniex.com
のtradingApi
を取得成功する必要があります。
は、あなたは必ず「[ 『エラー』:無効なコマンド。]」は、正確な印刷ラインから来ていますか? – pedrouan
@pedrouanはい。私はこのトピックを書くときに "print"コマンドを削除しました。しかし、すでにそれを編集する。 – VladyslavPG
よく。応答は正しく表示されるため、エラーはAPIに関連していません。 'print(dataBack.response)'の直後にこの行を追加してください: 'print(dataBack)' – pedrouan