I want to access twitter filter search v2 endpoint in ios app

I want to access twitter filter search v2 endpoint https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/integrate/recovery-and-redundancy-features in ios app can you tell me how can I access the stream. the normal api call does not seems to work. However the python sample code https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/main/Filtered-Stream/filtered_stream.py seems to work but I am unable to achieve it in ios. Can you guide me towards an example that involves embedding python or ruby code inside ios app?

Replies

Can you guide me towards an example that involves embedding python or ruby code inside ios app?

You should not need to embed Python or Ruby inside an iOS app. Taking a look at the Python sample that you provided, it looks like you need to create a HTTP POST request that uses a Bearer token to provide authorization to items in the feed. Now, you will need to consult the documentation from the API vendor on how you obtain this Bearer token, or how it is created, but once you have it you should be able to use URLSessionDataTask along with URLRequest to create the POST request to send to this endpoint.

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com

Ok, I tried this: " 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 }     let parameterDictionary = ["":""]     var request = URLRequest(url: serviceUrl)     request.httpMethod = "POST"     request.setValue("Bearer token", forHTTPHeaderField: "Authorization")     request.setValue("Application/json", forHTTPHeaderField: "Content-Type")     guard let httpBody = try? JSONSerialization.data(withJSONObject: parameterDictionary, options: []) else {       return     }     request.httpBody = httpBody           let session = URLSession.shared     session.dataTask(with: request) { (data, response, error) in       if let response = response {         print(response)       }       if let data = data {         do {           let json = try JSONSerialization.jsonObject(with: data, options: [])           print(json)         } catch {           print(error)         }       }     }.resume()   } " But got this error: " Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} 2021-09-20 22:53:27.037625+0400 TwitterSearch[2888:29990410] Task .<2> finished with error [-1001] Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x282503f90 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://api.twitter.com/2/tweets/search/stream?place.fields=contained_within&user.fields=location&tweet.fields=geo, NSErrorFailingURLKey=https://api.twitter.com/2/tweets/search/stream?place.fields=contained_within&user.fields=location&tweet.fields=geo, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.} 2021-09-20 22:53:27.038186+0400 TwitterSearch[2888:29990410] Task .<3> finished with error [-1001] Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x282539290 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://api.twitter.com/2/tweets/search/stream?place.fields=contained_within&user.fields=location&tweet.fields=geo, NSErrorFailingURLKey=https://api.twitter.com/2/tweets/search/stream?place.fields=contained_within&user.fields=location&tweet.fields=geo, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.} "

Bearer token seems to work fine it appears now "JSON text did not start with array or object and option to allow fragments not set." needs to be taken care of

Ok I did this " 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 = "POST"     request.setValue("Bearer MY TOKEN", forHTTPHeaderField: "Authorization")     request.setValue("text/html; charset=UTF-8", forHTTPHeaderField: "Content-Type")     request.setValue("gzip, deflate, br", forHTTPHeaderField: "Accept-Encoding")           let session = URLSession.shared     session.dataTask(with: request) { (data, response, error) in       if let response = response {         print(response)       }       if let data = data {         do {           let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)           print(json)         } catch let error {           print("error: (error)")         }       }     }.resume()   } " and now it is giving me this error error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}

Ok request.httpMethod is supposed to be "GET". Issue resolved. Thanks alot for the help Matt. I really appreciate it.

  • No problem at all.

Add a Comment

Matt now the issue seems to be this first time when I make request I get this:

[general] Connection to daemon was invalidated

Next time I get this: "connection_issue" = TooManyConnections;   detail = "This stream is currently at the maximum allowed connection limit.";   title = ConnectionException;   type = "https://api.twitter.com/2/problems/streaming-connection";"

using URLSession can there be such a way that the connection to the server remains throughout and does not shutdown?

There are a few things that could be happening here; First, regarding:

[general] Connection to daemon was invalidated

If you are using a background URLSessionConfiguration then nsurlsessiond could be rejecting your task for an unknown reason.

Next, regarding:

Next time I get this: "connection_issue" = TooManyConnections; detail = "This stream is currently at the maximum allowed connection limit.";

This looks like a general connection limit that you are exceeding based on the Vendor's SDK. I would checkout their documentation to see if there is a recommended refresh rate.

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com

@meaton In simple words i want to attain data via streaming in ios application exactly as being done here in this python example https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/main/Filtered-Stream/filtered_stream.py can you help me with this?