0

I am trying to post a buy or sell order through Poloniex's trading API, the problem is that I keep getting the following error:

{
    error = "Invalid API key/secret pair.";
}

The code for posting the order is the following:

func postOrder(type: String, price: String, amount: String) {

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

        if key != "" && secret != "" {
            guard let url = URL(string: "https://poloniex.com/tradingApi") else {return}

            let sign = "nonce=\(timeNow)&command=\(orderType.lowercased())&currencyPair=\(coinPair)&rate=\(price)&amount=\(amount)"

            let parameters: Parameters = ["command" : orderType.lowercased(), "currencyPair" : coinPair, "rate" : price, "amount" : amount, "nonce" : timeNow]

            let hmacSign: String = SweetHMAC(message: sign, secret: secret).HMAC(algorithm: .sha512)

            let headers: HTTPHeaders = ["key" : key, "sign" : hmacSign]

            print(parameters)
            print(sign)

            request(url, method: .post, parameters: parameters, encoding: URLEncoding.httpBody, headers: headers).responseJSON(completionHandler: {
                response in

                if let jsonResponse = response.result.value as? NSDictionary {

                    print(jsonResponse)

                    if let orderNumber = jsonResponse["orderNumber"] as? String {

                        print("placed order with id: " + orderNumber)

                        JSSAlertView().show(self, title: "Success", text: "The order has been placed", noButtons: false, buttonText: nil, cancelButtonText: "Ok", color: .white)

                        self.priceTextField.text = ""
                        self.amountTextField.text = ""

                    } else {
                        JSSAlertView().show(self, title: "Failure", text: "Not able to place order", noButtons: false, buttonText: nil, cancelButtonText: "Ok", color: .red)
                    }
                } else {
                    print("json is not readable")
                    JSSAlertView().show(self, title: "Failure", text: "Not able to place order", noButtons: false, buttonText: nil, cancelButtonText: "Ok", color: .red)
                }

            })

        } else {
            print("can't show balances because the key and secret are nil")

            JSSAlertView().show(self, title: "Log in", text: "Please log in before trying to place an order", noButtons: false, buttonText: nil, cancelButtonText: "Ok", color: .gray)
        }

    }

I used this function to access other data from their API, of course with the right command. But this time it does not want to work.

By shuffling the parameters inside the sign constant I would usually be able to make it work.

Their documentation is fairly poor and I have been researching this for while now. Any help is greatly appreciated. Thank you!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • `let headers: HTTPHeaders = ["Key" : key, "Sign" : hmacSign]` instead? Headers are case sensitives (https://stackoverflow.com/questions/5258977/are-http-headers-case-sensitive) and it seems that they are starting with an uppercase (https://poloniex.com/support/api/). Or, is it in fact `let headers: HTTPHeaders = [key: hmacSign]`, which'd make more sense before "key" is weird as a "real key". – Larme Sep 20 '17 at 16:42
  • I get the same error even if I use uppercase letters. Also using the exact same function I get back data from other commands. I am thinking that it’s the sign constant, I kept changing the order of the parameters but nothing seems to work. – Florin Alexandru Sep 20 '17 at 16:50

0 Answers0