6

I have recently started using Facebook SDK 3.1, and am encountering some problems with logging in using openActiveSessionWithReadPermissions.

Actually, loggin in works perfectly, if there is a cached token available, it logs in without presenting Facebook UI, and if not, it presents the UI.

The problem occurs after I make a call to reauthorizeWithPublishPermissions. If I call reauthorizeWithPublishPermissions, then close and reopen the application, and make a call to openActiveSessionWithReadPermissions, it presents the Facebook UI and requires the user to say "yes I'm OK with read permissions", even though there is a cached token available.

It only presents the Facebook UI erroneously if I make a call to reauthorizeWithPublishPermissions, otherwise everything works fine.

Open for read code:

[FBSession openActiveSessionWithReadPermissions:readpermissions allowLoginUI:YES
                                         completionHandler:^(FBSession *aSession, FBSessionState status, NSError *error) {
            [self sessionStateChanged:[FBSession activeSession] state:status error:error];
            if (status != FBSessionStateOpenTokenExtended) {
                // and here we make sure to update our UX according to the new session state
                FBRequest *me = [[FBRequest alloc] initWithSession:aSession
                                                         graphPath:@"me"];
                [me startWithCompletionHandler:^(FBRequestConnection *connection,
                                                 NSDictionary<FBGraphUser> *aUser,
                                                 NSError *error) {
                    self.user = aUser;

                    aCompletionBlock(aSession, status, error);
                }];
            }
        }];

the sessionStateChanged function:

- (void)sessionStateChanged:(FBSession *)aSession state:(FBSessionState)state error:(NSError *)error {

    if (aSession.isOpen) {
        // Initiate a Facebook instance and properties
        if (nil == self.facebook || state == FBSessionStateOpenTokenExtended) {
            self.facebook = [[Facebook alloc]
                             initWithAppId:FBSession.activeSession.appID
                             andDelegate:nil];

            // Store the Facebook session information
            self.facebook.accessToken = FBSession.activeSession.accessToken;
            self.facebook.expirationDate = FBSession.activeSession.expirationDate;
        }
    } else {
        // Clear out the Facebook instance
        if (state == FBSessionStateClosedLoginFailed) {
            [FBSession.activeSession closeAndClearTokenInformation];
        }
        self.facebook = nil;
    }
}

the Publish call, with an empty aPublishAction for testing:

- (void)doPublishAction:(void(^)(FBSession *aSession, NSError *error))aPublishAction {

    if ([FBSession.activeSession.permissions
         indexOfObject:@"publish_actions"] == NSNotFound) {

        NSArray *writepermissions = [[NSArray alloc] initWithObjects:
                                     @"publish_stream",
                                     @"publish_actions",
                                     nil];

        [[FBSession activeSession]reauthorizeWithPublishPermissions:writepermissions defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *aSession, NSError *error){
            if (error) {
                NSLog(@"Error on public permissions: %@", error);
            }
            else {
                aPublishAction(aSession, error);
            }


        }];

    }
    else {
        // If permissions present, publish the story
         aPublishAction(FBSession.activeSession, nil);
    }
}

Thanks for everything in advance, I would be grateful for any and all help!!

Jake Mogwai
  • 96
  • 1
  • 1
  • 5
  • I think publish_actions is all that you need. I think it now encompasses publish_stream. – SAHM Mar 29 '13 at 23:28
  • never found solution, we actually stopped requiring publish permissions, so we never needed to find a solution. – Jake Mogwai Jan 07 '14 at 18:52

2 Answers2

6

You need to add

[FBSession.activeSession handleDidBecomeActive];

to your -(void) applicationDidBecomeActive:(UIApplication *)application method in your app's delegate as stated in the migration guide.

  • I do have that line set, the function looks like this: `- (void)applicationDidBecomeActive:(UIApplication *)application { [FBSession.activeSession handleDidBecomeActive]; }` to clarify, the problem happens when I terminate and reopen the application. – Jake Mogwai Nov 20 '12 at 21:44
  • Hello. So, as far as I understand, is there any chance of `+[FBsession openActiveSessionWithReadPermissions:allowLoginUI:completionHandler:]` getting called again while the session is already open? If this ever happens, the default behaviour is to close the session and try to open it again, which would explain the issue. – Alejandro Benito-Santos Nov 21 '12 at 12:26
  • I can confirm that there is no chance of this. As I have said, this issue only occurs when `reauthorizeWithPublishPermissions` is called in the previous application session. This leads me to believe that this is what is causing the problem. – Jake Mogwai Nov 21 '12 at 18:12
-2

What if you use openActiveSessionWithPermissions instead of openActiveSessionWithReadPermissions?

Biga
  • 531
  • 4
  • 9
  • Thats what I had originally, and it worked perfectly. But as I understand it though, that is a depreciated method from Facebook SDK 3.0, and that to switch to the new system of Facebook SDK 3.1, one must request read and write permissions separately, using `openActiveSessionWithReadPermissions` to request read, and `reauthorizeWithPublishPermissions` to request write. It is on this new system that I am having trouble. – Jake Mogwai Nov 26 '12 at 15:24
  • 1
    openActiveSessionWithPermissions is deprecated. – gran33 Feb 03 '14 at 13:14