如何以ios最佳方式获取facebook accessToken?

时间:2014-08-29 13:30:24

标签: ios iphone facebook-access-token

我尝试了下面的代码,但它正在处理错误。

- (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的最佳方式是什么?感谢。

1 个答案:

答案 0 :(得分: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];              
                      }
                  }];
             }
         }];