检查 accesstoken 是不是过期 Facebook SDK 4.7 ios

Posted

技术标签:

【中文标题】检查 accesstoken 是不是过期 Facebook SDK 4.7 ios【英文标题】:Check if accesstoken is expired Facebook SDK 4.7 ios检查 accesstoken 是否过期 Facebook SDK 4.7 ios 【发布时间】:2016-01-29 06:17:48 【问题描述】:

我正在使用 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 提供了一个过期日期。这有什么帮助?该设备可能有错误的日期。

【问题讨论】:

您可以从网络获取当前日期和时间,并根据该日期和时间做出决定。 【参考方案1】:

假设用户之前已经用 Facebook 登录过并且有[FBSDKAccessToken currentAccessToken] != nil(这里我不再赘述,因为通过 FB 登录是另一回事)。

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

为简单起见,以下所有代码都在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 令牌的好时机(例如,某个应用程序在后台运行了一段时间)时,请致电 -requestFacebookUserInfo。这将提交 Open Graph 请求并在令牌无效/过期时返回错误。

【讨论】:

【参考方案2】:

用于检查 facebook 权限..& 授予权限...如果权限存在则自动获取 accesstoken 其他明智的要求登录...

对于斯威夫特

 var login: FBSDKLoginManager = FBSDKLoginManager()
login.logInWithReadPermissions(["public_profile", "email"], handler:  (result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in

  if (error != nil)
  
        //Process error
   
   else if result.isCancelled
   
        //Handle cancellations
   
  else
  
        // If you ask for multiple permissions at once, you
        // should check if specific permissions missing
        if result.grantedPermissions.contains("email")
            //Do work
   
     
   )

对于目标 c:

像这样检查权限。以下代码使用..

if ([[FBSDKAccessToken currentAccessToken]hasGranted:@"email"])
       
          // add your coding here after login call this block automatically.
       
       else
       

    //login code  **//if accesstoken expired...then call this block**

    FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];

    [loginManager logInWithReadPermissions:@[@"public_profile", @"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)




      ];

       

【讨论】:

hasGranted 是否确保访问令牌未过期?能否提供您的信息来源? 如果访问令牌过期,则要求登录

以上是关于检查 accesstoken 是不是过期 Facebook SDK 4.7 ios的主要内容,如果未能解决你的问题,请参考以下文章

access token已过期

Spring Security Oauth2:处理过期 AccessToken 的流程

如何获取accessToken

通过 Instagram API 访问公共 Instagram 内容而不使 accesstoken 过期

Oauth2;如何解决多个异步 api 调用期间 AccessToken 过期的问题,同时进行?

当 accessToken 已过期且客户端需要发送刷新令牌时,应该向客户端发送哪个状态码