Question

In my iOS app I use with the Social Framework the Facebook API to make request to share with Facebook, but now I receive this status code from Facebook:

{
    error =     {
        code = 190;
        "error_subcode" = 463;
        message = "Error validating access token: Session has expired on 25 marzo 2014 16.20. The current time is 18 aprile 2014 8.45.";
        type = OAuthException;
    };
}

This is the code i use:

ACAccountType *facebookTypeAccount = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

NSDictionary *options = @{ACFacebookAppIdKey: @"##myfbid###",ACFacebookPermissionsKey: @[@"email"]};

[accountStore requestAccessToAccountsWithType:facebookTypeAccount
    options:options
    completion:^(BOOL granted, NSError *error) {
        if(granted){
            NSArray *accounts = [accountStore accountsWithAccountType:facebookTypeAccount];
            facebookAccount = [accounts lastObject];
            //NSLog(@"Success");

            NSURL *meurl = [NSURL URLWithString:@"https://graph.facebook.com/me"];
            SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                requestMethod:SLRequestMethodGET
                URL:meurl
                parameters:nil];
            merequest.account = facebookAccount;
            [merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
                //this json conaints the log i write above
        }

];

So i have tried to logout from the Facebook account in the iPhone Setting, and then login again, so my question is there is a way to automatically refresh this token every time the app open to avoid this problem?

Was it helpful?

Solution

You cannot "automatically", refresh the access token. Whenever you get the response, you should check for the error and if error exists renew the token. This code may help you-

....
if([json.profileDictionary objectForKey:@"error"]!=nil){
   [self renewCredentials]; 
}

-(void)renewCredentials{
    [self.accountStore renewCredentialsForAccount:(ACAccount *)self.facebookAccount completion:^(ACAccountCredentialRenewResult renewResult, NSError *error){
        if(!error){
            switch (renewResult) {
                case ACAccountCredentialRenewResultRenewed:
                    NSLog(@"Good to go");
                    [self getFacebookAccount];
                    break;
                case ACAccountCredentialRenewResultRejected:
                    NSLog(@"User declined permission");
                    break;
                case ACAccountCredentialRenewResultFailed:
                    NSLog(@"non-user-initiated cancel, you may attempt to retry");
                    break;
                default:
                    break;
            }
        }
        else{
            //handle error
            NSLog(@"error from renew credentials%@",error);
        }
    }];
}

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top