提供的Facebook会话令牌已过期或无效

时间:2016-03-11 03:45:51

标签: ios swift facebook facebook-graph-api parse-platform

我试图用我的Swift应用程序实现Facebook登录。我能够很好地登录到应用程序。但是,一旦我将用户注销,我就遇到了问题。我收到错误the supplied Facebook session token is expired or invalid。如何生成新令牌?这是代码:

      if let accessToken: FBSDKAccessToken = FBSDKAccessToken.currentAccessToken() {
        PFFacebookUtils.logInInBackgroundWithAccessToken(accessToken, block: {
            (user: PFUser?, error: NSError?) -> Void in
            if user != nil {
                print("User logged in through Facebook!")

                self.navigateToInGame(true)

            } else {
                print("Uh oh. There was an error logging in.")

                let manager = FBSDKLoginManager()
                manager.logOut()
                manager.logInWithReadPermissions(["public_profile", "email"], fromViewController: self, handler: { (result, error) -> Void in
                    if let error = error {
                        print(error.localizedDescription)
                    }

                    if result.isCancelled {
                        print("Cancelled")
                    } else {
                        print("Logged in")
                        if let user = user {
                            if user.isNew {
                                print("User signed up and logged in through Facebook!")

                                self.performSegueWithIdentifier("signUp", sender: self)

                            } else {
                                print("User logged in through Facebook!")

                                self.navigateToInGame(true)
                            }
                        } else {
                            print("Uh oh. The user cancelled the Facebook login.")
                        }

                    }
                })

                if accessToken.expirationDate.compare(NSDate()) == NSComparisonResult.OrderedAscending {
                    FBSDKLoginManager.renewSystemCredentials({ (result, error) -> Void in
                        if result != .Renewed {
                            // Do further procedures once renewed is failed or rejected
                            print("Token didn't renew")
                        }
                    })
                }

            }
        })
    } else {
        let permissions = ["public_profile", "email"]
        PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions, block: {
            (user: PFUser?, error: NSError?) -> Void in
            if let user = user {
                if user.isNew {
                    print("User signed up and logged in through Facebook!")

                    self.performSegueWithIdentifier("signUp", sender: self)

                } else {
                    print("User logged in through Facebook!")

                    self.navigateToInGame(true)
                }
            } else {
                print("Uh oh. The user cancelled the Facebook login.")
            }
        })
    }
}

更新代码。

我在日志中得到以下内容:

"哦,错误登录"。仍允许我通过FB登录。我点击"继续"在Facebook。

日志读取"登录"

日志读取"哦,用户取消了Facebook登录,它将我踢回登录界面。但是,用户仍然在Parse中创建。我想在第一次登录后转到我的signUp segue。

1 个答案:

答案 0 :(得分:1)

if result.isCancelled {
    print("Cancelled")
} else {
    result.token // Your new token is here and throw the token into logInInBackgroundWithAccessToken(:) again
}
//else {
//    print("Logged in")
//    if let user = user {
//        if user.isNew {
//            print("User signed up and logged in through Facebook!")
//            
//            self.performSegueWithIdentifier("signUp", sender: self)
//            
//        } else {
//            print("User logged in through Facebook!")
//            
//            self.navigateToInGame(true)
//        }
//    } else {
//        print("Uh oh. The user cancelled the Facebook login.")
//    }
//    
//}

部分if let user = user { ... }对我没有意义,因为result.token刚刚获得了新的令牌func logInWithReadPermissions。因此,更新后的令牌应再次通过PFFacebookUtils.logInInBackgroundWithAccessToken(:)