0

我得到 -error = "Invalid API key/secret pair.";

当我在 chrome 的 REST 客户端中尝试 POST 请求时: 在此处输入图像描述

我得到正确的 json 信息: 在此处输入图像描述

但是在带有 Alamofire 的 Swift 中,我尝试执行相同的请求并收到错误...

我的代码:

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

0 回答 0