Chada
Chada

Reputation: 63

How to refresh Facebook access token automatically in an iPhone app and Android app?

I am developing a product for iPhone and Android for Facebook users. Since "offline_access" permissions had been removed from Facebook Graph API, and the life time of an access token can only extended to 60 days, I want to know if there is a way to refresh access token automatically. Anyone has a good idea?

Upvotes: 3

Views: 1658

Answers (2)

Prince Kumar Sharma
Prince Kumar Sharma

Reputation: 12641

In ios you can extend token using following methods:

- (void)extendAccessToken {
    if (_isExtendingAccessToken) {
        return;
    }
    _isExtendingAccessToken = YES;
    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"auth.extendSSOAccessToken", @"method", nil];
    [self requestWithParams:params andDelegate:self];
}

//+ * Calls extendAccessToken if shouldExtendAccessToken returns YES.

- (void)extendAccessTokenIfNeeded {



    if ([self shouldExtendAccessToken]) {
        [self extendAccessToken];
    }
}

//  Returns YES if the last time a new token was obtained was over 24 hours ago.

- (BOOL)shouldExtendAccessToken {
        if ([self isSessionValid]){
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *components = [calendar components:NSHourCalendarUnit fromDate:_lastAccessTokenUpdatetoDate:[NSDate date]options:0];

        if (components.hour >= kTokenExtendThreshold) {
            return YES;
        }
    }
    return NO;
}

as given in this Link

Upvotes: 1

Kapil Vats
Kapil Vats

Reputation: 5515

You no need to care about refresh the token, facebook sdk will take care of that automatically

Upvotes: 1

Related Questions