Swift Twitter搜索API无法验证您的响应,错误

时间:2018-12-18 11:16:01

标签: ios swift api twitter alamofire

当我第一次调用api时,得到了预期的响应。但是,一旦我更改了params值,响应就会显示为“ message”:“无法对您进行身份验证:

var params = ["q":"fashion"]

var header = ["authorization":"OAuth 
oauth_consumer_key=\"consumer_key\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"1545130242\",oauth_nonce=\"nonce\",oauth_version=\"1.0\",oauth_signature=\"signature\""]

Alamofire.request(url, method: .get, parameters: params,headers: headers).responseJSON { (response) in
        if (response.result.isSuccess){
            print("Success! got the weather data")
            let twitterJSON: JSON = JSON(response.result.value!)
            // self.updateWeatherData(json: weatherJSON)
            print("\(twitterJSON)")
            self.updateTwittes(json: twitterJSON)
        }else{
            print("Error \(String(describing: response.result.error)) ")
        }
    }

1 个答案:

答案 0 :(得分:1)

应使用BrearerToken调用TwitterAPI请求,该请求可通过curl命令获取:

        let params = ["query":"\(searchText) lang:en","maxResults":"10"]
        let headers = ["authorization":"bearer <BrearerToken>","content-type":"application/json"]
        getTwitterData(url: url,headers: headers ,Params: params)

func getTwitterData(url: String,headers: [String:String] ,Params: [String: String]){


    Alamofire.request(url, method: .post, parameters: Params,encoding:JSONEncoding.default,headers: headers).responseJSON { (response) in
        if (response.result.isSuccess){
            print("Success! got the twitter data")
            let twitterJSON: JSON = JSON(response.result.value!)

            print("\(twitterJSON)")
            self.updateTwittes(json: twitterJSON)
        }else{
            print("Error \(String(describing: response.result.error)) ")
        }
    }
}