3

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

4

2 回答 2

1

您无需关心刷新令牌,facebook sdk 会自动处理

于 2013-02-22T10:16:17.023 回答
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;
}

如本链接所示

于 2013-02-22T10:27:12.523 回答