iOS - FBSession& FBRequestMe获取用户数据问题

时间:2014-05-05 06:47:13

标签: ios facebook

我使用Facebook iOS SDK v3.13.1让用户登录他们的Facebook帐户。我正在获取用户使用的Facebook ID。

首先,我使用它:

FBSession *session = [[FBSession alloc] init];
// Set the active session
[FBSession setActiveSession:session];
// Open the session
[session openWithBehavior:FBSessionLoginBehaviorWithFallbackToWebView completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
    }
}];

...要登录,如果没有错误,请致电[[FBRequest requestForMe] startWithCompletionHandler:]以获取用户数据。

但它似乎没有完美运行,有些用户可以成功登录,但有些用户无法登录。

大部分错误都是(由Flurry记录):

FBSessionStateClosedLoginFailed
com.facebook.sdk:UserLoginCancelled

然后,我发现[FBSession openWithBehavior]的文件写道:

  

会话不能与其他类一起使用   SDK直到它打开。如果,在打开之前,会话在   FBSessionStateCreatedTokenLoaded>状态,然后没有UX发生,和   会话可供使用。

这是否意味着我无法将此方法与FBRequest一起使用,或者它会导致错误?

我应该使用FBSession openActiveSessionWithReadPermissions吗?

1 个答案:

答案 0 :(得分:0)

accessTokan = [[[FBSession activeSession] accessTokenData] accessToken];
                 //NSLog(@"%@",accessTokan);


             NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
             [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@",accessTokan]]];
             [request setHTTPMethod:@"GET"];
             [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
             NSError *error;
             NSURLResponse *response;
             NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
             NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

             NSLog(@"%@",str);
             fb_dict = [str JSONValue];
             [str release];

             NSLog(@"%@",fb_dict);