5
votes

I am using TwitterKit and Firebase to allow a user to login using Twitter. The first time I attempt to login with Twitter it fails with error:

[TwitterCore] Cannot verify session credentials. 2018-07-11 13:57:20.999365-0400 social_notes[6794:1892888] [TwitterKit] did encounter error with message "Failed to save session": Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x1c044dcb0 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=57, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=https://api.twitter.com/1.1/account/verify_credentials.json, NSErrorFailingURLKey=https://api.twitter.com/1.1/account/verify_credentials.json, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=57, NSLocalizedDescription=The network connection was lost.} 2018-07-11 13:57:21.413753-0400 social_notes[6794:1892888] [TwitterKit] did encounter error with message "Error obtaining user auth token.": Error Domain=TWTRLogInErrorDomain Code=-1 "Callback URL not approved for this client application. Approved callback URLs can be adjusted in your application settings" UserInfo={NSLocalizedDescription=Callback URL not approved for this client application. Approved callback URLs can be adjusted in your application settings} Twitter Login Error: Request failed: forbidden (403)

But then if I just try to login with Twitter a second time, it works without issue.

Info.plist

<dict>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>twitterkit-XXX</string>
            </array>
        </dict>
    </array>
    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>twitter</string>
        <string>twitterauth</string>
    </array>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>armv7</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
</dict>

App Delegate

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()

        window?.rootViewController = UINavigationController(rootViewController: ViewController())

        FirebaseApp.configure()

        Twitter.sharedInstance().start(withConsumerKey:"XXX", consumerSecret:"XXX")

        return true
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return Twitter.sharedInstance().application(app, open: url, options: options)
    }

Twitter Login Button Code

 let twitterLoginBtn = TWTRLogInButton { (session, err) in
            if let err = err {
                print("Twitter Login Error: \(String.init(describing: err.localizedDescription))")
                return
            }
            guard let token = session?.authToken else { return }
            guard let secret = session?.authTokenSecret else { return }
            let credential = TwitterAuthProvider.credential(withToken: token, secret: secret)

            Auth.auth().signIn(with: credential, completion: { (user, err) in

etc etc....
1
I am having the same issue! Did you find anything?BlackM
@BlackM It is because the user isn't signed out (don't know why it doesn't work sometimes). So I basically just triple sign out the user. I have a view controller with a logout button that then takes you to another view controller and then from there you can sign in with Twitter. So I log the user out of Twitter when they click the "logout" button, when they land on the login view controller, and when they click to sign in with Twitter. This verifies they are for sure logged out (probably overkill but it works)Connor V

1 Answers

1
votes

This is clearly a TwitterKit issue. This is how I solved it:

    let store = TWTRTwitter.sharedInstance().sessionStore
    store.reload()
    for case let session as TWTRSession in store.existingUserSessions() {
        store.logOutUserID(session.userID)
    }