Facebook token storage for offline access

I'm looking for a way to save an access token so that a user can post to Facebook without having to log in for every API call:

I need an offline_access token, I store it in NSUserDefaults, but when I try to use it again, I get a FacebookErrDomain 10000 error

That's what I'm doing:

In fbDidLogin I get access_token and keep it default for users

- (void)fbDidLogin { NSString *token = self.facebook.accessToken; [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"facebookToken"]; } 

After that, when I run the application again, I just get the token from the user defaults and assign them to the facebook object:

 NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"facebookToken"]; [_facebook setAccessToken:token]; 

But that does not work.

Does anyone know what I can do wrong?

Thanks, Vincent.

EDIT: If I do an NSlog after [[NSUserDefaults standardUserDefaults] objectForKey:@"facebookToken"]; , I will see that the token is saved.

+1
source share
3 answers

I had the same problem and found out that the problem is that you are not storing the expiration date.

If you do this, it will allow you to redo it in order.

Code example:

  [[NSUserDefaults standardUserDefaults] setObject:_facebook.accessToken forKey:@"fb_access_token"]; [[NSUserDefaults standardUserDefaults] setObject:_facebook.expirationDate forKey:@"fb_exp_date"]; 

To get the same thing, just set _facebook.expirationDate .

+7
source

Well, it looks like this is an iOS SDK framework that is buggy.

Using the token stored in the user's default settings, I can write to the wall using curl:

 curl -F 'access_token=br69pK_lh0Xbj....plDRUdG97a55KIHzlaiw' \ -F 'message=TEST API.' \ https://graph.facebook.com/ME/feed 

So, I'm just doing HTTPS POST, so using ASIHTTPRequest, this code works like a charm:

 NSURL* faceboobUrl = [NSURL URLWithString:@"https://graph.facebook.com/ME/feed"]; self.request = [ASIFormDataRequest requestWithURL:faceboobUrl]; [request setRequestMethod:@"POST"]; [request setPostValue:token forKey:@"access_token"]; [request setPostValue:msg forKey:@"message"]; [request setDelegate:self]; [request setTimeOutSeconds:TIMEOUT]; [request startAsynchronous]; 

No thanks facebook;)

Vincent

+3
source

Be sure to call:

 [[NSUserDefaults standardUserDefaults] synchronize]; 

This will โ€œsaveโ€ your preferences to the / flash drive.

Also do an NSLog on the token before storing it to make sure it is not null.

+2
source

Source: https://habr.com/ru/post/1347839/


All Articles