1

Once the User's Facebook Access Token is Invalidated for any reason (either deleting the app from the Facebook website or deleting the app's access from the Facebook settings), I Can't manage to Get a NEW TOKEN, no matter what I Do!

So Far, I've tried two methods: (sharedFaceBookManager is a Singleton that Manages Facebook)

1) [sharedFaceBookManager.facebook extendAccessToken];

This Works for the First time the "Invalidation" happens, the App will open FB's App and Prompt the User for whatever is Required and return to the App Safe and sound... The SECOND time the Access token is Invalid, extendAccessToken will just Hang there and the user will no longer be able to login through Facebook. Not a good Solution.

2) Attempting to Delete Cookies, Disconnect form Facebook, and Re-Connect!

for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {

    if([[cookie domain] isEqualToString:@"facebook"]) {

        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
}

// Reconnecting:
NSArray *permissions = [NSArray arrayWithObjects:@"email", @"read_friendlists", @"user_photos", @"user_events", nil];
[FBSession openActiveSessionWithReadPermissions:permissions
                                   allowLoginUI:YES
                              completionHandler:
 ^(FBSession *session,
   FBSessionState state, NSError *error) {
     [[EWFacebookManager sharedFaceBookManager] sessionStateChanged:session state:state error:error];
 }];

This Only returns a Call to - (void)sessionStateChanged: with:FBSessionStateOpenand the Apps Deals with this as if Login Succeeded, Result: ENDLESS LOOP (Login Success - Login Failed) NOT A GOOD SOLUTION!

I Know I Shouldn't call extendAccessToken when the Token is invalid, I Should Prompt the User to Login Again

ANYBODY? Please? We've even turned to an outsourcing company to help us deal with this and NO BREAK THROUGH!

Mani
  • 17,549
  • 13
  • 79
  • 100
Hernan Arber
  • 1,626
  • 3
  • 22
  • 35

2 Answers2

5

SOLVED!!!

So, the Conclusion: You CAN'T Extend an Invalid Facebook Access Token. Instead, you should Clean your AccessToken and Renew your Facebook Credentials Via Your FBSession.

  • First I Disconnect from Facebook and clean my token data.
  • Then I Renew My Credentials for the FBSession
  • Finally I Try to Reconnect by opening a New Active Session.

Here is my Code ("Invalid fb Token" will call "renewFacebookCredentials" and afterwards "facebookReconnect" will be executed):

- (void)facebookReconnect
{
    NSArray *permissions = [NSArray arrayWithObjects:@"email", @"read_friendlists", @"user_photos", @"user_events", nil];
    [FBSession openActiveSessionWithReadPermissions:permissions
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session,
       FBSessionState state, NSError *error) {
         [[EWFacebookManager sharedFaceBookManager] sessionStateChanged:session state:state error:error];
     }];

}

+ (void)renewFacebookCredentials
{
    [FBSession.activeSession closeAndClearTokenInformation];
    [FBSession renewSystemCredentials:^(ACAccountCredentialRenewResult result,
                                        NSError *error)
     {
         [[EWFacebookManager sharedFaceBookManager] facebookReconnect]; 
     }];
}

I Hope this Helps some lost souls and prevent more hatred towards the new Facebook iOS SDK and their "Weak" Documentation ;-)

Hernan

Adi
  • 1,263
  • 1
  • 13
  • 24
Hernan Arber
  • 1,626
  • 3
  • 22
  • 35
0

You should code like this,

1) This code will execute , after user login finished...(If it's place in appdelegate means, that is a good idea)

if( SYSTEM_VERSION_LESS_THAN(@"6.0"))
        [[self facebook] extendAccessTokenIfNeeded];
    else
        if( [[FBSession activeSession] respondsToSelector:@selector(handleDidBecomeActive)])
            [FBSession.activeSession handleDidBecomeActive];

2) When you invoke facebook, you should validate session, otherwise it will lead to error. Code should:

if (![[self.appDelegate facebook] isSessionValid]) {
            NSArray *permissions = [[NSArray alloc] initWithObjects:@"offline_access", nil];
            [[self.appDelegate facebook] authorize:permissions];
            [permissions release];
        }
Mani
  • 17,549
  • 13
  • 79
  • 100
  • Hi Mani: I Think "offline_access" Permissions were already Deprecated by Facebook. Are you sure this could be the Solution? I AM Handling the "handleDidBecomeActive" on the AppDelegate. But I Don't know where I Should VALIDATE the FB Session. – Hernan Arber May 20 '13 at 13:43
  • @HernanArber In my old project , I used permission like this, if you used updated facebook.sdk, You have to use new one. You have to validate session on first line of button click or method fired event where you wish to invoke facebook for any event.(`i.e. appDelegate.facebook`). – Mani May 20 '13 at 13:48