2014-08-29 76 views
0

我嘗試了下面的代碼,但它正在處理錯誤。如何以ios最佳方式獲取facebook accessToken?

- (void) openActiveSessionWithPermissions: (NSArray *) arr 
{ 
    if ([FBSession activeSession].state == FBSessionStateCreatedTokenLoaded){ 
     [self openWithUI:NO permissions:arr]; 
    } else if ([FBSession activeSession].state == FBSessionStateOpen || [FBSession activeSession].state == FBSessionStateOpenTokenExtended){ 
     [[FBSession activeSession] closeAndClearTokenInformation]; 
    } else { 
     [[FBSession activeSession] closeAndClearTokenInformation]; 
     [self openWithUI:YES permissions:arr]; 
    } 
} 

- (void) openWithUI: (BOOL) UI permissions: (NSArray *) permissions 
{ 
    [FBSession openActiveSessionWithReadPermissions:permissions allowLoginUI:UI completionHandler:^(FBSession *session, FBSessionState state, NSError *error) { 

     if (error) { 
      [self.delegate didReceieveError:[error description]]; 
     } else { 
      [self.delegate didTakeAccessToken:session.accessTokenData.accessToken]; 
     } 
    }]; 
} 

我找不到我的錯誤。 什麼是accesToken的最佳途徑?謝謝。

+0

如果'openActiveSessionWithPermissions聲明:'看起來'openActiveSessionWithReadPermissions陌生和完成處理程序:'會被多次調用。閱讀文件。 – kelin 2014-08-29 13:36:49

回答

0

試試這個:

[FBSession openActiveSessionWithReadPermissions:@[@"basic_info", @"email"] 
              allowLoginUI:YES 
             completionHandler: 
     ^(FBSession *session, FBSessionState state, NSError *error) 
     { 
      if (FBSession.activeSession.isOpen) 
      { 
       [[FBRequest requestForMe] startWithCompletionHandler: 
        ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) 
        { 
         if (!error) 
         { 
          //Get AccessToken        
          NSString *aStrFBAccessToken = [[[FBSession activeSession] accessTokenData] accessToken];    
         } 
        }]; 
      } 
     }]; 
+0

我試過了,但它確實檢查accessToken是否有效 – 2014-08-29 13:51:11