iOS Parse Facebook Login error 308 FBSDKLoginBadChallengeString

后端 未结 21 2444
小鲜肉
小鲜肉 2020-12-13 17:18

I\'m trying to implement Facebook login in my iOS app, using Parse.

let permissionsArray = [\"public_profile\", \"email\"]

PFFacebookUtils.logInInBackgroun         


        
相关标签:
21条回答
  • 2020-12-13 18:07

    Fixed it. This issue was caused by logging in with Parse and with FBSDKLoginButton.

    Here's what I was doing:

    //in my UI setup code:
    let fbLoginButton = FBSDKLoginButton()
    fbLoginButton.addTarget(self, action: "fbButtonTouched:", forControlEvents: UIControlEvents.TouchUpInside)
    
    ....
    
    func fbButtonTouched() {
        let permissionsArray = ["public_profile", "email"]
    
        PFFacebookUtils.logInInBackgroundWithReadPermissions(permissionsArray)
            { (user: PFUser?, error: NSError?) in
    
                if let user = user {
                    //we logged in!
                }
                else {
                    //login error!
                }
        }
    }
    

    I found out (the hard way) that tapping a FBSDKLoginButton attempts to log the user in! There was no need to also log the user in using PFFacebookUtils.logInInBackgroundWithReadPermissions. So I just got rid of fbButtonTouched() and I could happily log in.

    So what happened was that I was attempting two logins at the same time, which the simulator was happy with, but not the device.

    If you want to know why, it's because the login authentication challenge it received did not correspond to the one it was expecting because it received the one from the first log in attempt after it had sent the one from the second log in attempt and was therefore waiting for the second log in attempts' authentication challenge.

    The documentation for FBSDKLoginButton doesn't currently mention anything about the fact that tapping it initiates the log in flow, and because it's a subclass of UIButton, you'd think that you ought to addTarget to it and implement the login flow yourself. And having two logins at the same time leads to a misleading FBSDKLoginBadChallengeString. Recipe for disaster...

    EDIT: The documentation has been updated correspondingly:

    FBSDKLoginButton works with [FBSDKAccessToken currentAccessToken] to determine what to display, and automatically starts authentication when tapped (i.e., you do not need to manually subscribe action targets).

    (my emphasis)

    0 讨论(0)
  • 2020-12-13 18:07

    Follow these steps to resolve that kind of error :

    1. Go to your project
    2. Click Capabilities
    3. Enable Keychain Access
    0 讨论(0)
  • 2020-12-13 18:08

    Try running your app straight from your device without having to launch it from your Xcode. Then login to Facebook w/o being bothered by 308. After that, you can resume launching from the Xcode to your device w/o the same issue.

    This seems an issue between the FB SDK and iOS and not related to Parse. I'd appreciate it if someone could enlighten us on this subject.

    My Xcode's version is 7.1, FB SDK is 4.8.x.

    0 讨论(0)
  • 2020-12-13 18:08

    It's due to the authenticationChallenge issue when you mistakenly try to login twice simultaneously and then further logins will give you some keychain warning on console while debugging, this error will be noticeable when you will be debugging on device. Without debugging mode you'll not get this error, the reason is keychain will work fine when you are not debugging.

    There are few workarounds for this: 1. Try logging in without debugging mode and once it succeeded you can further login successfully with debugging mode too. 2. Check for error code FBSDKLoginBadChallengeString and call [FBSDKAccessToken setCurrentAccessToken:nil] to clear token and perform login again.

    0 讨论(0)
  • 2020-12-13 18:09

    A simple solution work for me. Just enable Keychain Sharing in Capabilities

    0 讨论(0)
  • 2020-12-13 18:09

    I got the same problem when I was trying to catch login and logout event as below

    loginButton = [[FBSDKLoginButton alloc] init];
    // Handle clicks on the button
    [loginButton
     addTarget:self
     action:@selector(loginButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    // Once the button is clicked, show the login dialog
    -(void)loginButtonClicked
    {
      FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
      [login
        logInWithReadPermissions: @[@"public_profile"]
              fromViewController:self
                         handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error) { 
          NSLog(@"Process error");
        } else if (result.isCancelled) {
          NSLog(@"Cancelled");
        } else {
          NSLog(@"Logged in");
        }
      }];
    }
    

    But this is only good for a custom login button, if you are using default fb login button, the FBSDKLoginManager has been initialized already, you will get the error when trying to create another one.

    So, in order to catch login and logout event, just set the delegate for login button and then handle it like the following:

        loginButton = [[FBSDKLoginButton alloc] init];
        loginButton.delegate = self;
    
    
    - (void)loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error {
        if (!error) {
            [self getFBProfile];
        }
    }
    
    - (void)loginButtonDidLogOut:(FBSDKLoginButton *)loginButton {
        [self logout];
    }
    
    0 讨论(0)
提交回复
热议问题