16

我正在按照本教程 在 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
                                     }
                                 }];
}

出现错误:错误:

域 = com.facebook.sdk,代码 = 5

4

2 回答 2

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(注意复数)。

于 2013-02-04T21:37:18.183 回答
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 将打开。

于 2013-07-29T07:08:07.120 回答