[BUG] Twitter Kit iOS 3.1.0 guest REST calls sometimes fail with Unable to verify your credentials


#1

Every once and awhile, making a guest api call via TWTRAPIClient returns the following error:

Domain=TwitterAPIErrorDomain Code=99 “Request failed: forbidden (403)” UserInfo={NSLocalizedFailureReason=Twitter API error : Unable to verify your credentials (code 99), TWTRNetworkingStatusCode=403, NSErrorFailingURLKey=https://api.twitter.com/oauth2/token, NSLocalizedDescription=Request failed: forbidden (403)}

I’ve been printing out the HTTP headers when this happens, and its looks like there is no active guest session at the time this occurs. For some reason the SDK can’t or isn’t establishing one? Notice there is no x-guest-token or Authorization header.

“User-Agent” = “Fabric/X.Y.Z (Convene_Dev/3.5.0; iOS Simulator (iPhone); iOS 10.2.0 (16G29) iOS; Scale/2.00) TwitterKit/3.1.0”;

When this condition happens, it persists no matter how many api request attempts are made. Killing the app and restarting it fixes the problem. If I look for error code 99, is there any SDK calls I can make to get things going again? Perhaps something like:

    let sessionStore = Twitter.sharedInstance().sessionStore
    sessionStore.fetchGuestSession { (session, error) in
        if let session = session {
            //Now we have a guest session, try REST call again?
        }
    };

Reproduction steps:

  1. Call Twitter.sharedInstance().logIn(completion: )

  2. Make Guest REST api request:

     let client = TWTRAPIClient()
     let url = "https://api.twitter.com/1.1/statuses/user_timeline.json"
     var clientError : NSError?
     let request = client.urlRequest(withMethod: "GET", url: url, parameters: ["screen_name" : "devtester6", "count" : "25"], error: &clientError)
     print(request.allHTTPHeaderFields ?? "")
     client.sendTwitterRequest(request, completion: { [weak self] (response, data, error) in
    
     }
    
  3. Sometimes you get this error: Domain=TwitterAPIErrorDomain Code=99 “Request failed: forbidden (403)” UserInfo={NSLocalizedFailureReason=Twitter API error : Unable to verify your credentials (code 99), TWTRNetworkingStatusCode=403, NSErrorFailingURLKey=https://api.twitter.com/oauth2/token, NSLocalizedDescription=Request failed: forbidden (403)}

  4. Once this error occurs, any number of retries at the api call fail

  5. Killing the app and restarting it resolves the issue.


#2

Any ideas here? I put in a check for error code 99 which then calls sessionStore.fetchGuestSession() with a retry of the originating request in the completion block. I don’t know if this will work but since this problem is so hard to reproduce I’m going to give it a try.