I'm trying to read messages from a user's facebook inbox, but I'm hitting an error that refuses me access. This is my code:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *fbAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options = @{
ACFacebookAppIdKey : FacebookAppKey,
ACFacebookPermissionsKey : FacebookPermissionsArray
};
[accountStore requestAccessToAccountsWithType:fbAccountType options:options completion:^(BOOL granted, NSError *error)
{
if (error || !granted) {
return;
}
NSArray *accounts = [accountStore accountsWithAccountType:fbAccountType];
if (accounts.count == 0) {
return;
}
ACAccount *facebookAccount = [accounts lastObject];
NSURL *inboxURL = [NSURL URLWithString:@"https://graph.facebook.com/me/inbox"];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
SLRequest *facebookInfoRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:inboxURL
parameters:parameters];
[facebookInfoRequest setAccount:facebookAccount];
[facebookInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"response data: %i, url response: %@, error: %@", responseData.length, urlResponse, error);
}];
}];
The URL response to this reads:
<NSHTTPURLResponse: 0xcb5fa10> { URL: https://graph.facebook.com/me/inbox?access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX } { status code: 400, headers {
"Access-Control-Allow-Origin" = "*";
"Cache-Control" = "no-store";
Connection = "keep-alive";
"Content-Length" = 194;
"Content-Type" = "application/json; charset=UTF-8";
Date = "Tue, 18 Feb 2014 15:29:36 GMT";
Expires = "Sat, 01 Jan 2000 00:00:00 GMT";
Pragma = "no-cache";
"Www-Authenticate" = "OAuth \"Facebook Platform\" \"invalid_token\" \"Error validating access token: Session has expired on Feb 13, 2014 6:17am. The current time is Feb 18, 2014 7:29am.\"";
"X-FB-Debug" = "XXXXXXXXXXXXXXXX/Y+K0w=";
"X-FB-Rev" = XXXXXXXX;
} }
I'm unsure why this problem has happened. What could I be doing wrong here?
The Facebook account on the device has become out-of-sync with the server and SDK's cache.
To force an update just call ACAccountStore's
method renewCredentialsForAccount
.
- (void)refreshFacebookTokenStatus
{
ACAccountStore *accountStore;
ACAccountType *accountTypeFB;
if ((accountStore = [[ACAccountStore alloc] init]) &&
(accountTypeFB = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]))
{
NSArray *fbAccounts = [accountStore accountsWithAccountType:accountTypeFB];
id account;
if (fbAccounts && [fbAccounts count] > 0 && (account = [fbAccounts objectAtIndex:0]))
{
[accountStore renewCredentialsForAccount:account
completion:^(ACAccountCredentialRenewResult renewResult, NSError *error)
{
if (error)
{
NSLog(@"%@", error.localizedDescription);
}
}];
}
}
}
The simplest would be to call it each time your app starts but this might not be efficient. So it's better to call it when the session state change occurs.
Also make sure your Facebook app settings do not have the Sandbox
option on if you're trying to login with a non-app-developer Facebook user.