Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FBSession: an attempt was made reauthorize permissions on an unopened session

I'm following this tutorial to implement the view preview post on Facebook SDK 3.1, but when I call this method ...

// Ask for publish_actions permissions in context
    if ([FBSession.activeSession.permissions
         indexOfObject:@"publish_actions"] == NSNotFound) {
        // No permissions found in session, ask for it
        [FBSession.activeSession
         reauthorizeWithPublishPermissions:
         [NSArray arrayWithObject:@"publish_actions"]
         defaultAudience:FBSessionDefaultAudienceFriends
         completionHandler:^(FBSession *session, NSError *error) {
             if (!error) {
                 // If permissions granted, publish the story
                 [self publishStory];
             }
         }];
    } else {
        // If permissions present, publish the story
        [self publishStory];
    }

...

returns the following error:

* Terminating app due to uncaught exception 'com.facebook.sdk: InvalidOperationException', reason: 'FBSession: an attempt was made ​​reauthorize permissions on an unopened session'

What is happening can? Thank you!

EDIT: Ran my friend, thank you very much, but still have a detail ...When'll post the first time he asks to authorize the application I authorize this block of fall

/* * open a new session with publish permission */

  [FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
                                   defaultAudience:FBSessionDefaultAudienceOnlyMe
                                      allowLoginUI:YES
                                 completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                                     if (!error && status == FBSessionStateOpen) {
                                         [self publishStory];
                                     }else{
                                         NSLog(@"error");
                                        //Here I get the error mentioned below
                                     }
                                 }];
}

with the error: error:

domain = com.facebook.sdk, code = 5

like image 668
jucajl Avatar asked Feb 04 '13 20:02

jucajl


2 Answers

The error says that the FBSession is not opened. so you should check if the session is opened before trying to reauthorize.

if ([[FBSession activeSession] isOpen]) {
  /* 
   * if the current session has no publish permission we need to reauthorize 
   */
  if ([[[FBSession activeSession] permissions]indexOfObject:@"publish_actions"] == NSNotFound) {

        [[FBSession activeSession] requestNewPublishPermissions:[NSArray arrayWithObject:@"publish_actions"] defaultAudience:FBSessionDefaultAudienceFriends
                                              completionHandler:^(FBSession *session,NSError *error){
                                                  [self postPhoto];
                                              }];

    }else{
        [self publishStory];
    }
}else{
    /* 
     * open a new session with publish permission 
     */
    [FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
                                       defaultAudience:FBSessionDefaultAudienceOnlyMe
                                          allowLoginUI:YES
                                     completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                                         if (!error && status == FBSessionStateOpen) {
                                             [self publishStory];
                                         }else{
                                             NSLog(@"error");
                                         }
                                     }];
}

Make sure to consistently request for the same permissions which should be publish_actions (mind the plural).

like image 196
oiledCode Avatar answered Oct 31 '22 23:10

oiledCode


You also need another method in AppDelegate:

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    // attempt to extract a token from the url
    return [FBAppCall handleOpenURL:url
                  sourceApplication:sourceApplication
                    fallbackHandler:^(FBAppCall *call) {
                        NSLog(@"In fallback handler");
                    }];
}

This will handle the url callback if the authentication is done in safari. After that the FBSession will be open.

like image 3
PowerQian Avatar answered Oct 31 '22 22:10

PowerQian