gpt4 book ai didi

ios - 检查accesstoken是否过期Facebook SDK 4.7 ios

转载 作者:可可西里 更新时间:2023-11-01 05:20:02 45 4
gpt4 key购买 nike

我正在使用 facebook sdk 4.7,我需要检查 accesstoken 是否过期。

FBSDKAccessToken *access_token = [FBSDKAccessToken currentAccessToken];
if (access_token != nil) {
//user is not logged in

//How to Check if access token is expired?
if ([access_token isExpired]) {
//access token is expired ......
//
}
}

如果我成功了,我必须再次登录用户。

SDK 提供了一个到期日期。这有什么帮助?设备可能有错误的日期。

最佳答案

假设用户之前已经登录过 Facebook 并且拥有 [FBSDKAccessToken currentAccessToken] != nil(我在这里不做详细介绍,因为通过 FB 登录是另一回事)。

在我的应用中,我执行以下操作以确保 FB 访问 token 始终有效并与我的应用服务器同步。

为简单起见,下面的所有代码都在AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...

/**
Add observer BEFORE FBSDKApplicationDelegate's
application:didFinishLaunchingWithOptions: returns

FB SDK sends the notification at the time it
reads token from internal cache, so our app has a chance
to be notified about this.
*/
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(fbAccessTokenDidChange:)
name:FBSDKAccessTokenDidChangeNotification
object:nil];

return [[FBSDKApplicationDelegate sharedInstance] application: application didFinishLaunchingWithOptions: launchOptions];
}

- (void)fbAccessTokenDidChange:(NSNotification*)notification
{
if ([notification.name isEqualToString:FBSDKAccessTokenDidChangeNotification]) {

FBSDKAccessToken* oldToken = [notification.userInfo valueForKey: FBSDKAccessTokenChangeOldKey];
FBSDKAccessToken* newToken = [notification.userInfo valueForKey: FBSDKAccessTokenChangeNewKey];

NSLog(@"FB access token did change notification\nOLD token:\t%@\nNEW token:\t%@", oldToken.tokenString, newToken.tokenString);

// initial token setup when user is logged in
if (newToken != nil && oldToken == nil) {

// check the expiration data

// IF token is not expired
// THEN log user out
// ELSE sync token with the server

NSDate *nowDate = [NSDate date];
NSDate *fbExpirationDate = [FBSDKAccessToken currentAccessToken].expirationDate;
if ([fbExpirationDate compare:nowDate] != NSOrderedDescending) {
NSLog(@"FB token: expired");

// this means user launched the app after 60+ days of inactivity,
// in this case FB SDK cannot refresh token automatically, so
// you have to walk user thought the initial log in with FB flow

// for the sake of simplicity, just logging user out from Facebook here
[self logoutFacebook];
}
else {
[self syncFacebookAccessTokenWithServer];
}
}

// change in token string
else if (newToken != nil && oldToken != nil
&& ![oldToken.tokenString isEqualToString:newToken.tokenString]) {
NSLog(@"FB access token string did change");

[self syncFacebookAccessTokenWithServer];
}

// moving from "logged in" state to "logged out" state
// e.g. user canceled FB re-login flow
else if (newToken == nil && oldToken != nil) {
NSLog(@"FB access token string did become nil");
}

// upon token did change event we attempting to get FB profile info via current token (if exists)
// this gives us an ability to check via OG API that the current token is valid
[self requestFacebookUserInfo];
}
}

- (void)logoutFacebook
{
if ([FBSDKAccessToken currentAccessToken]) {
[[FBSDKLoginManager new] logOut];
}
}

- (void)syncFacebookAccessTokenWithServer
{
if (![FBSDKAccessToken currentAccessToken]) {
// returns if empty token
return;
}

// BOOL isAlreadySynced = ...
// if (!isAlreadySynced) {
// call an API to sync FB access token with the server
// }
}

- (void)requestFacebookUserInfo
{
if (![FBSDKAccessToken currentAccessToken]) {
// returns if empty token
return;
}

NSDictionary* parameters = @{@"fields": @"id, name"};
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me"
parameters:parameters];

[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSDictionary* user = (NSDictionary *)result;
if (!error) {
// process profile info if needed
}
else {
// First time an error occurs, FB SDK will attemt to recover from it automatically
// via FBSDKGraphErrorRecoveryProcessor (see documentation)

// you can process an error manually, if you wish, by setting
// -setGraphErrorRecoveryDisabled to YES

NSInteger statusCode = [(NSString *)error.userInfo[FBSDKGraphRequestErrorHTTPStatusCodeKey] integerValue];
if (statusCode == 400) {
// access denied
}
}
}];
}

每次您认为是检查 FB token 的好时机(例如,应用程序在后台运行了一段时间),请调用 -requestFacebookUserInfo。如果 token 无效/过期,这将提交 Open Graph 请求并返回错误。

关于ios - 检查accesstoken是否过期Facebook SDK 4.7 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33408600/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com