How to access twitter filter search stream api on ios

125 Views Asked by At

This is the approach I am using to get the tweets via twitter new v2 filter search API after getting the bearer token.

static func callFilterSearch() {
    let Url = String(format: "https://api.twitter.com/2/tweets/search/stream?place.fields=contained_within&user.fields=location&tweet.fields=geo")
    guard let serviceUrl = URL(string: Url) else { return }
    var request = URLRequest(url: serviceUrl)
    request.httpMethod = "GET"
    request.setValue("Bearer MY BEARER TOKEN", forHTTPHeaderField: "Authorization")
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("gzip, deflate, br", forHTTPHeaderField: "Accept-Encoding")
    request.setValue("keep-alive", forHTTPHeaderField: "Connection")
    request.setValue("*/*", forHTTPHeaderField: "Accept")
    request.setValue("v2FilteredStreamPython", forHTTPHeaderField: "User-Agent")
    let sessionConfig = URLSessionConfiguration.default
    sessionConfig.networkServiceType = .responsiveData
    let session = URLSession(configuration: sessionConfig)
    session.dataTask(with: request) { (data, response, error) in
        if let data = data {
            do {
                let teamJSON = try JSONSerialization.jsonObject(with: data, options: [.allowFragments, .fragmentsAllowed, .mutableContainers, .mutableLeaves])
                print("teamJSON: \(teamJSON)")
            } catch let error {
                print("error: \(error)")
            }
        }
    }.resume()
}

Unfortunately I am not successful in getting the tweets this way. However the python client that twitter has put in github repo seems to work just fine and is spewing tweets as expected.

Please help me out here

0

There are 0 best solutions below