如何在iPhone应用程序和Android应用程序中自动刷新Facebook访问令牌?

时间:2013-02-22 10:12:33

标签: android iphone ios facebook

我正在为Facebook用户开发适用于iPhone和Android的产品。由于已经从Facebook Graph API中删除了“offline_access”权限,并且访问令牌的生命周期只能延长到60天,我想知道是否有办法自动刷新访问令牌。任何人都有个好主意?

2 个答案:

答案 0 :(得分:1)

你无需关心刷新令牌,facebook sdk会自动解决这个问题

答案 1 :(得分:1)

在ios中,您可以使用以下方法扩展令牌:

- (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;
}

在此Link

中给出