Why is the accesstoken not set by PFFacebookUtils logInInBackgroundWithReadPermissions method?

515 views Asked by At

For reference: I am using the latest Facebook IOS SDK v4 and the latest Parse v1.7.4 and ParseFacebookUtilsV4 SDK.

So I am using PFFacebookUtils loginInBackgroundWithReadPermissions:block: method to have the user login with Facebook credentials and create a PFUser with those credentials. And then proceeded to make a facebook graph request with FBSDKGraphRequest which from what i read, assumes a valid token has been set (there is no passing of a token parameter to it).

But the facebook graph request failed, and in tracing the issue, I noticed that the "access token" is never set by the loginInBackgroudnWithReadPermissions method. What this method only does is to create a PFUser and a Session instance in Parse, and store there the session token string, but it does not set the currentAccessToken . When I do a [[FBSDKAccessToken currentAccessToken] tokenString] call within the block, I get (null). But if I read the "token string" from the PFSession class I get the "token string" store in the Session class instance in Parse.

See the code below:

- (IBAction)fbLoginAction:(id)sender {

// Set permissions required from the facebook user account
NSArray *permissionsArray = @[ @"email", @"user_friends"];
// Login PFUser using Facebook
[PFFacebookUtils logInInBackgroundWithReadPermissions:permissionsArray block:^(PFUser *user, NSError *error) {
    if (!user) {
        NSLog(@"Uh oh. The user cancelled the Facebook login.");
    } else if (user.isNew) {
        NSLog(@"User signed up and logged in through Facebook!");


        // Check if the current token has been set
        NSLog(@"self.myCurrentToken string = %@", [[FBSDKAccessToken currentAccessToken] tokenString]); // returns (null)

        // Get the token string from the PFSession
        [PFSession getCurrentSessionInBackgroundWithBlock:^(PFSession *session, NSError *error) {
                    NSString *tokenString = session.sessionToken;
                    NSLog(@"Session token = %@", tokenString);

                }];

}

And here is the debug console output enter image description here

So I am not sure what is happening. IF the PFSession token is set to something shouldn't the FBSDKAccessToken currentAccessToken not be returning this same PFsession token?

And if I were to set the accessToken manually, how can I convert the tokenstring that is stored in Parse to an FBSDKAccessToken instance? There seems to be no method to do this?

thanks

2

There are 2 answers

0
malena On

@jhk:

Yes. I got an explanation from Facebook support. Login in through Parse is a two step process. First step, is the authorization step, where the user authenticates with Facebook. It switches to Facebook app, and the app receives Facebook access token when it completes successfully. And the second step is , the app authenticates with Parse (i.e your app) using the Facebook token. If it matches existing user/session, you are logged in successfully. However, I was deleting the PFSession manually from the parse backend, while testing every single time I tried to login, so that invalidated the session stored in the backend, and after the 1st step, when the app gets authorized and gets the accesstoken, the system realizes that the local session doesn't match the parsed stored session and invalidated the accesstoken, it sets it to nil. That is why I am seeing the token being set to nil.

0
jhk On

This is happening because you need to add the following code to the bottom of AppDelegate didFinishLaunchingWithOptions :

return [[FBSDKApplicationDelegate sharedInstance] application:application
                                    didFinishLaunchingWithOptions:launchOptions];

By adding the code, your currentAccessToken will no longer be nil and you can make requests to FB right away.

FYI, I was able to discover the acccessToken was nil because I got the following error every time I tried to use FBSDKGraphRequest. So if anyone else gets the error below, make sure your accessToken is not nil even after you log in!

{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}