facebook sdk ios expiration date
I'm trying to record token and expiration date of facebook for not authorize user every time. But tonight I got this error an error like: {"error":{"type":"OAuthException"
.
First let me explain how and when I record expiration date and token:
In DidLogin
:
-(void)fbDidLogin{
[[NSUserDefaults standardUserDefaults]setValue:facebook.accessToken forKey:@"fb_access_token"];
[[NSUserDefaults standardUserDefaults]setObject:facebook.expirationDate forKey:@"fb_expiration_date"];
And in login:
facebook.accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"fb_access_token"];
facebook.expirationDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"fb_expiration_date"];
if (![facebook isSessionValid]) {
NSLog(@"session not valid authorize again");
[facebook authorize:_permissions delegate:self];
}
else
{
if(开发者_运维技巧[delegate respondsToSelector:@selector(didFB_login)])[delegate didFB_login];
self.loggedin=TRUE;
}
After some NSLog
I understood the problem:
My expiration date is
exp date:4001-01-01 00:00:00 +0000
So for ensure I'm not falling in mistake I modified facebook.m
:
//FBLoginDialogDelegate
/**
* Set the authToken and expirationDate after login succeed
*/
- (void)fbDialogLogin:(NSString *)token expirationDate:(NSDate *)expirationDate {
self.accessToken = token;
self.expirationDate = expirationDate;
NSLog(@"self.expirationDate:%@",self.expirationDate);
if ([self.sessionDelegate respondsToSelector:@selector(fbDidLogin)]) {
[_sessionDelegate fbDidLogin];
}
}
then forced a new authorize call but the result is still:
self.expirationDate:4001-01-01 00:00:00 +0000
Now the question is: is there a bug in Facebook OAuth? How can I be sure that problem is not in my date saving code?
FB is deprecating the use of "offline_access" for long lived sessions. Please refer to https://developers.facebook.com/docs/offline-access-deprecation/ for more information.
And here: https://developers.facebook.com/docs/mobile/ios/build/#extend_token if you're developing in iOS.
Cheers!
Damn after some debug i found that handleOpenURL query:access_token=accesstokenstring&expires_in=0
Why OAuth answer me expires_in 0 ? offcourse the date is 4001 cause
// We have an access token, so parse the expiration date.
NSString *expTime = [params valueForKey:@"expires_in"];
NSLog(@"expTime:%@",expTime);
NSDate *expirationDate = [NSDate distantFuture];
if (expTime != nil) {
int expVal = [expTime intValue];
if (expVal != 0) {
expirationDate = [NSDate dateWithTimeIntervalSinceNow:expVal];
expVal is 0 so expiration date is distantFuture. But token was expiried and i got oauth expection when i try to query with that token.. So? Anyone has idea?
Solved.. If anyone want to know why this happen this is the answer: The expires_in was 0 due to offline_access permission.. Burt token was expiried server side due to facebook password change. To fix this just add in
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error{
NSLog(@"didFailWithError : %@",[error description]);
NSDictionary* userinfo=[error userInfo];
// NSLog(@"userinfo:%@",userinfo);
NSString *type=[[userinfo valueForKey:@"error"]valueForKey:@"type"];
// NSLog(@"type:%@",type);
if([type isEqualToString:@"OAuthException"]){
NSLog(@"Exception from oauth let's take new token");
[facebook authorize:_permissions delegate:self];
}
精彩评论