Nat
Nat

Reputation: 12942

FBSession fails to open for the first time

I've got a problem when i try to open facebook session for the first time - i get facebook error 5 (not authorised). In fact, i do not get this popup where user is asked for permissions. However if i stop the app and run it again, everything works fine (i see the popup and there is no fb error 5). I can't find any reason for such behavior.

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
}

Error 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)>}

Upvotes: 0

Views: 521

Answers (1)

ChenSmile
ChenSmile

Reputation: 3441

Add the following code along with ur code. Help it will resolve ur problem.

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

Go to AppDelegate.m File and Add the given below Code-

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

Upvotes: 4

Related Questions