Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TwitterKit Login always fails first try then succeeds second try (Swift, TwitterKit, Firebase)

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....
like image 485
Connor V Avatar asked Nov 08 '22 04:11

Connor V


1 Answers

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)
    }
like image 68
BlackM Avatar answered Nov 15 '22 06:11

BlackM