FBSession:尝试重新授权未打开的会话的权限

时间:2013-02-04 20:45:01

标签: ios objective-c facebook sdk

我正在关注此tutorial 在Facebook SDK 3.1上实现视图预览帖子,但是当我调用此方法时 ...

// 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];
    }

...

返回以下错误:

  

* 由于未捕获的异常'com.facebook.sdk:InvalidOperationException'而终止应用程序,原因:'FBSession:尝试进行了   重新授权未打开的会话'

发生了什么事可以?谢谢!

修改 我的朋友,非常感谢你,但仍然有一个细节...当他第一次要求授权申请时,我会授权这块秋天

/ *  *使用发布权限打开一个新会话  * /

  [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
                                     }
                                 }];
}

错误:错误:

  

domain = com.facebook.sdk,code = 5

2 个答案:

答案 0 :(得分:48)

错误表明FBSession未打开。所以你应该在尝试重新授权之前检查会话是否已经打开。

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");
                                         }
                                     }];
}

确保始终要求相同的权限,应该是 publish_actions (请注意复数形式)。

答案 1 :(得分:3)

您还需要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");
                    }];
}

如果在safari中完成身份验证,这将处理url回调。之后,FBSession将会公开。