Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

ios - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...