Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I simulate an expired access token in iOS Facebook SDK 3.1?

Tags:

ios

facebook

I would like to test the code path where a Facebook user's access token has expired and I need to renew the token. I noticed that the access token expires 2 months after it has been created. I don't want to wait 2 months to test that code path, so I would like a way to simulate this expiration. I am scared that my code will crash if I never test this situation for the following mysteries:

  1. Currently upon app launch, I call [FBSession openActiveSessionWithReadPermissions:permArray allowLoginUI:NO completionHandler:someFunction] to silently reconnect a Facebook user that has already connected in the past. Notice how allowLoginUI is NO. Does it have to be YES to allow the user to re-login and renew the token or can the token be silently renewed?
  2. The completion handler of any openActiveSession* call is triggered every time the session state changes. Two notable states are FBSessionStateOpen and FBSessionStateOpenTokenExtended. When the token becomes extended, does the state machine remain in the token extended state, or will it go to the extended state and then immediately go to the open state? I need to know the state transition, so I don't run my handlers twice.
  3. Does [FBSession activeSession].accessToken become nil or does it remain as the old expired token?
  4. Similarly for [FBSession activeSession].expirationDate: is it nil, the old expiration date, or automatically becomes new expiration date?
like image 534
Pwner Avatar asked Sep 28 '12 17:09

Pwner


1 Answers

To make a token expired, the easiest way is to log into FB on your computer, go to App Center, click "My Apps", and remove your app by clicking the small X next to your app. This will cause the token to become "expired". Once this happens, you cannot automatically renew the token without interaction from the user. You must re-open the authentication UI and the user must click "Allow"

In regards to your questions:

  1. allowLoginUI must be YES. If the token expires, the user must interact with the UI before you can get a new token and setting it to NO will have it fail silently.

  2. I believe the TokenExtended state would only happen when extending a currently active token. If a user uses your app before the 2 month period is over, the FB SDK will automatically extend the token for your periodically. This is not the same as renewing an expired token though. Not 100% sure of this though, as I haven't tested this state.

  3. If your token expires, when you next launch the app you should call openActiveSession in one of your AppDelegate methods (didFinishLaunching) and this will trigger the completion handler with a state of either Closed or LoginFailed. At this point, you should be calling [FBSession.activeSession closeAndClearTokenInformation]; to nil out your access token, per their tutorial.
  4. Same as #3
like image 107
stipe108 Avatar answered Oct 18 '22 13:10

stipe108