TwitterKit登录总是第一次尝试失败,然后第二次尝试成功(Swift、TwitterKit、Firebase)



我使用TwitterKit和Firebase允许用户使用Twitter登录。我第一次尝试使用Twitter登录时失败,出现错误:

[TwitterCore]无法验证会话凭据。2018-07-11 13:57:20.999365-0400 socialnotes[6794:1892888][TwitterKit]确实遇到错误,消息为"保存会话失败":错误域=NSURLError域代码=-1005"网络连接丢失。"UserInfo={NSUnderlyingError=0x1c044dcb0{错误域=kCFErrorDomainCFNetwork代码=-100 5"(null("UserInfo={_kCFStreamErrorCodeKey=57,_kCFSstreamErrorDomainKey=1}},NSErrorFailingURLStringKey=https://api.twitter.com/1.1/account/verify_credentials.json,NSErrorFailing_URLKey=https://ape.twitter.com/s1.1/account/verify_credentials.json,_kCFStreamErrorDomainKey=1,_kCFStreamErrorCodeKey=57,NSLocalizedDescription=网络连接丢失。}2018-07-11 13:57:21.413753-0400 social_notes[6794:1892888][TwitterKit]确实遇到错误,消息为"获取用户身份验证令牌时出错。":错误域=TWTRLogInErrorDomain代码=-1"未批准此客户端应用程序的回调URL。已批准的回调URL可以在您的应用程序设置中调整"UserInfo={NSLocalizedDescription=此客户端应用程序的回调URL未被批准。已批准的回拨URL可以在应用程序设置中将其调整}Twitter登录错误:请求失败:禁止(403(

但如果我只是尝试第二次登录Twitter,它就可以正常工作。

信息列表

<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>

应用程序代表

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登录按钮代码

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....

这显然是一个TwitterKit问题。我就是这样解决的:

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

最新更新