0

I get - error = "Invalid API key/secret pair.";

When I try a POST request in REST client in chrome: enter image description here

I get the correct json information: enter image description here

But in Swift with Alamofire I try to do the same request and get an error...

My code:

func getRawJSON(method: String) {
        let publicKey = "YF9RCYRE-GL29DI0T-8GE62O2X-9OQ21A2P"
        let secretKey = "79aef0ae2bb54df5c5a4e6c28757ddf54a184964fb8c978b5770895944ca7778b582ff390dffdf073a77aac1de1ea1a793dfa6629c3394465345d31a62f953e9"
        let APIURL = "https://www.poloniex.com/tradingApi"

        let timeNowInt = Int(NSDate().timeIntervalSince1970)
        let timeNow = String(timeNowInt)

        let query = NSURLComponents()
        query.queryItems = [NSURLQueryItem(name: "command", value: method) as URLQueryItem,
                            NSURLQueryItem(name: "nonce", value: timeNow) as URLQueryItem]

        let requestString = query.query!
        let params = [
            "command": method,
            "nonce:": timeNowInt
            ] as [String : Any]

        let codering = requestString.hmac(algorithm: .SHA512, key: secretKey)

        let headers = [
            "Sign": codering,
            "Key": publicKey,
            "Content-Type":"application/x-www-form-urlencoded"] as [String : String]
        print(headers)
        Alamofire.request(APIURL, withMethod: .post, parameters: params, encoding: .url, headers: headers)
            .responseJSON { response in
                print(response)
        }
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
VladyslavPG
  • 557
  • 1
  • 8
  • 19
  • If you hard code the Sign header does it work? – AMAN77 Sep 06 '16 at 18:00
  • For a POST request is both `parameters` and URL `query` required? Normally you use `query` for GET and `parameters` for POST. – vadian Sep 06 '16 at 18:03
  • @AMAN77 Does not work... Code : `Alamofire.request(APIURL, withMethod: .post, parameters: ["Sign": "323840f3a402e2346cc3cc0ac72da9bb1121261b1c3b013309ec15134a8745a5624160b4abe31613c7cbf375e3c68ff184b8027939d8efa05ea5154376aea15f", "Content-Type": "application/x-www-form-urlencoded", "Key": "YF9RCYRT-GL29DI0T-8CE62O2X-9OQ11A2P"], encoding: .url, headers: ["command": "returnBalances", "nonce:": "1473182825"]) .responseJSON { response in print(response) }` – VladyslavPG Sep 06 '16 at 18:06
  • @vadian Yes. From documentation: _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._ – VladyslavPG Sep 06 '16 at 18:10
  • The URL query specified by `NSURLQueryItems`creates the URL syntax `http://.....?key1=value1&key2=value2`. I doubt that is intended for a POST request. – vadian Sep 06 '16 at 18:13
  • This query sign with HMAC-SHA512 method and send in "Sign" key inside "headers". It is correct, because in REST client this code is work. – VladyslavPG Sep 06 '16 at 18:17
  • In the headers yes but most likely not in the literal URL. – vadian Sep 06 '16 at 18:20
  • @vadian Can you write little example? I think I not understand something – VladyslavPG Sep 06 '16 at 18:29
  • As mentioned above `NSURLQueryItems` are appended to the URL as `?key1=value1&key2=value2`. They are used in GET requests and are normally not required in POST requests because the information is sent via the parameters. You are using both. – vadian Sep 06 '16 at 18:34
  • Understand. But without "Data form" info (on the first picture in Rest Client), I get error - "Invalid command" from API – VladyslavPG Sep 06 '16 at 18:38

0 Answers0