Swift 3,Alamofire json获取“error =”无效的API密钥/密钥对。“”

时间:2016-09-06 17:45:52

标签: swift alamofire poloniex

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

当我在chrome中的REST客户端尝试POST请求时 enter image description here

我收到了正确的json信息: enter image description here

但是在Swift with Alamofire中我尝试做同样的请求并得到错误......

我的代码:

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

0 个答案:

没有答案