Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

idelovski

macrumors regular
Original poster
Sep 11, 2008
235
0
What would be the proper way to handle OAuth access token between two sessions?

In Facebook's SDK example they don't even bother. The documentation says that if user has official Facebook application on his device and if device is capable of multitasking then the "magic" in Facebook application will take care of everything.

But what if user doesn't have or even doesn't want the Facebook app on his device? Should I save access token myself in NSUserDefaults? I found example in Facebook Application Development For Dummies, but they store only facebook.accessToken so the thing wasn't working properly at first. Then I played with the code for a while. After looking inside a few of Facebook SDK methods I decided to store facebook.expirationDate too and now the code seem to work.

But googling around didn't confirm it as a smart long term solution so I'm asking here if someone has more experience with Facebook development. Should I store accessToken/expirationDate or not?

EDIT - Here's the code I have now:

Code:
//Called when the user successfully logged in.
- (void)fbDidLogin
{
   NSLog (@"did login");
   
   [[NSUserDefaults standardUserDefaults] setObject:facebook.accessToken
                                             forKey:kFacebookAccessToken];
   [[NSUserDefaults standardUserDefaults] setObject:facebook.expirationDate
                                             forKey:kFacebookExpirationDate];
	
   ...
}

//Called when the user logged out.
- (void)fbDidLogout
{
   NSLog (@"did logout");
	
   [[NSUserDefaults standardUserDefaults] removeObjectForKey:kFacebookAccessToken];
   [[NSUserDefaults standardUserDefaults] removeObjectForKey:kFacebookExpirationDate];

   ...
}

And, here is the method from the book, bold lines added by me:

Code:
- (NSString *)facebookAccessToken
{
   if (facebook.accessToken)
      return (facebook.accessToken);
   
   NSString  *savedToken = [[NSUserDefaults standardUserDefaults] objectForKey:kFacebookAccessToken];
   
   if (savedToken)  {
      facebook.accessToken = savedToken;
      [B]facebook.expirationDate = [[NSUserDefaults standardUserDefaults] objectForKey:kFacebookExpirationDate];
      if (![facebook isSessionValid])
         facebook.accessToken = nil;[/B]
   }
   
   return (facebook.accessToken);
}
 
Last edited:
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
That looks right to me. And yes, if you are requesting "offline_access" permission then you need to store the access token. otherwise it doesn't matter as the user would have to sign in every time anyway.

depending on what you want to do you may be interested in ShareKit. It's a drop in for multiple social networks.
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.