FBSession第一次无法打开

时间:2014-02-14 09:28:18

标签: ios objective-c facebook

当我第一次尝试打开facebook会话时遇到了问题 - 我得到了Facebook错误5(未经授权)。事实上,我没有得到用户被要求获得权限的弹出窗口。但是,如果我停止应用程序并再次运行它,一切正常(我看到弹出窗口,没有fb错误5)。我找不到任何这种行为的理由。

if ([FBSession.activeSession isOpen]) {
    [FBSession openActiveSessionWithReadPermissions:@[@"basic_info", @"email"] allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
            if (error) {NSLog(@"error"); return;}
            [self sessionStateChanged:session state:state error:nil];
        }];
}

- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState)state error:(NSError *)error {
switch (state) {
    case FBSessionStateOpen: {
        [FBSession setActiveSession:session];
        [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
            NSLog(@"test");
        }
        break;
    }
// do something else
}

错误5:

Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x165b1b10 {com.facebook.sdk:HTTPStatusCode=400, com.facebook.sdk:ParsedJSONResponseKey={
body =     {
    error =         {
        code = 190;
        "error_subcode" = 458;
        message = "Error validating access token: The user has not authorized application 1111111111.";
        type = OAuthException;
    };
};
code = 400;
}, com.facebook.sdk:ErrorSessionKey=<FBSession: 0x1664ad70, state: FBSessionStateClosed, loginHandler: 0x0, appID: 111111111, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0x16576320>, expirationDate: (null), refreshDate: (null), attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(null)>}

1 个答案:

答案 0 :(得分:4)

将以下代码与您的代码一起添加。帮助它解决您的问题。

-(void)openSession
{
NSArray *permissions = [[NSArray alloc] initWithObjects:
                        @"user_birthday",
                        @"friends_birthday",
                        nil];
[FBSession openActiveSessionWithReadPermissions:permissions
                                   allowLoginUI:YES
                              completionHandler:
 ^(FBSession *session,
   FBSessionState state, NSError *error)
{
  //[self.sessionStateChanged:session state:state error:error];
  }];
}

转到AppDelegate.m文件并添加以下代码 -

-(void)sessionStateChanged:(FBSession *)session
                  state:(FBSessionState) state
                  error:(NSError *)error
{
switch (state){
    case FBSessionStateOpen:
        if (!error)
        {
        // We have a valid session
        NSLog(@"User session found");
        }

        break;
        case FBSessionStateClosed:
        NSLog(@"sesion close");

        break;
        case FBSessionStateClosedLoginFailed:
        NSLog(@"sesssion failed");

        [FBSession.activeSession closeAndClearTokenInformation];
        break;
     default:
        break;
}

[[NSNotificationCenter defaultCenter]
 postNotificationName:UIDocumentStateChangedNotification
 object:session];

if (error) {
    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Error"
                              message:error.localizedDescription
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
    [alertView show];
}
}


-(BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI
{

NSArray *permissions = [[NSArray alloc] initWithObjects:
                        @"user_location",
                        @"user_birthday",
                        @"user_likes",
                        @"friends_birthday",
                        nil];

return [FBSession openActiveSessionWithReadPermissions:permissions
                                          allowLoginUI:allowLoginUI
                                     completionHandler:^(FBSession *session,
                                                         FBSessionState state,
                                                         NSError *error)
                                                                    {
                                         [self sessionStateChanged:session
                                                             state:state
                                                             error:error];}];
}