0

我正在尝试通过 Poloniex 的交易 API 发布买入或卖出订单,问题是我不断收到以下错误:

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

发布订单的代码如下:

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)
        }

    }

我使用这个函数从他们的 API 访问其他数据,当然是使用正确的命令。但这一次它不想工作。

通过改组sign常量内的参数,我通常可以使其工作。

他们的文档相当差,我已经研究了一段时间。任何帮助是极大的赞赏。谢谢!

4

0 回答 0