I am looking for a way to save the access token to allow the user to post on facebook without having to log in for each call to the graph API :
I require an offline_access token, i store it in the NSUserDefaults, but when i try to use it again i get a FacebookErrDomain error 10000
Here is what i do :
In the fbDidLogin i get the access_token and save it in the user defaults
- (void)fbDidLogin {
NSString *token = self.facebook.accessToken;
[[NSUserDefaults standardUserDefaults] setObject:token forKey:@"facebookToken"];
}
After that when i run the app 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 it doesn't work..
Has anybody any idea what i could开发者_Go百科 be doing wrong ?
Thanks, Vincent.
EDIT :
If i do a NSlog after [[NSUserDefaults standardUserDefaults] objectForKey:@"facebookToken"];
i can see that the token was saved.
I was having the same problem and I found out that the issue is you are not storing expiration date.
If you do that, it will allow you to relogin in fine.
Sample code:
[[NSUserDefaults standardUserDefaults] setObject:_facebook.accessToken forKey:@"fb_access_token"];
[[NSUserDefaults standardUserDefaults] setObject:_facebook.expirationDate forKey:@"fb_exp_date"];
Retrieving is the same, just set the _facebook.expirationDate
.
Ok it seems that it's the facebook iOS SDK which is buggy.
Using the token stored in the user defaults, i can perform a post on my wall using curl :
curl -F 'access_token=br69pK_lh0Xbj....plDRUdG97a55KIHzlaiw' \
-F 'message=TEST API.' \
https://graph.facebook.com/ME/feed
So i just perform a 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
Make sure to call :
[[NSUserDefaults standardUserDefaults] synchronize];
This will "save" your preferences to disk/flash.
Also do an NSLog on the token before you save it to make sure it's not nil.
精彩评论