2012-10-18 37 views
2

我想創建一個Facebook連接按鈕,當我連接時我需要獲取FBGraphUser。我有我的按鈕下面的代碼:用我自己的UIButton獲取用戶的Facebook信息

-(IBAction)fbButtonClickHandler:(id)sender { 
    mainDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate]; 

    if (mainDelegate.fbSession.isOpen) { 
    [mainDelegate.fbSession closeAndClearTokenInformation]; // FB delog 
    } 
    else { 
    if (mainDelegate.fbSession.state != FBSessionStateCreated) { 
     mainDelegate.fbSession = [[FBSession alloc] initWithPermissions:[NSArray arrayWithObjects:@"email", @"publish_actions", @"user_birthday",nil]]; 
    } 
    // loggin on facebook 
    [mainDelegate.fbSession openWithCompletionHandler:^(FBSession *session, 
                FBSessionState status, 
                NSError *error) { 

     [fbButton setTitle:[self updateFbButtonLabel] forState:UIControlStateNormal]; 
     // reading the documentation i'm trying this to get the FBGraphUser 
     if(session.isOpen) { 
      [FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id<FBGraphUser> user, NSError *error) { 

        if (!error) { 
        NSString *userInfo = @""; 

        // Example: typed access (name) 
        // - no special permissions required 
        userInfo = [userInfo 
           stringByAppendingString: 
           [NSString stringWithFormat:@"Name: %@\n\n", 
           user.name]]; 

        // Example: typed access, (birthday) 
        // - requires user_birthday permission 
        userInfo = [userInfo 
           stringByAppendingString: 
           [NSString stringWithFormat:@"Birthday: %@\n\n", 
           user.birthday]]; 
        // Display the user info 
        NSLog(@"%@", userInfo); 
        } 
        NSLog(@" error: %@" , error); 
       }]; 
     } 
     } 
    }]; 
} 
} 

的問題是,當我叫startForMeWithCompletionHandler失敗:

 
    Error: HTTP status code: 400 
    FBSDKLog: Response : 
    The operation couldn’t be completed. (com.facebook.sdk error 5.) 
    (null) 
    error: Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed.     
    (com.facebook.sdk error 5.)" UserInfo=0x925f760 {com.facebook.sdk:ParsedJSONResponseKey= 
    {type = mutable dict, count = 2, 
    entries => 
     1 : {contents = "code"} = 
     {value = +400, type = kCFNumberSInt32Type} 
     2 : {contents = "body"} = {type = mutable dict, count = 1, 
     entries => 
     11 : {contents = "error"} = {type = mutable dict, count = 3, 
     entries => 
    2 : {contents = "type"} = {contents = "OAuthException"} 
     3 : {contents = "message"} = {contents = "An active access token must be used to query information about   
     the current user."} 
    6 : {contents = "code"} = 2500 

我缺少的東西?

回答

4

Aparently的FBRequestConnection使用FBSession變量稱爲活動會話,使其工作剛過

[mainDelegate.fbSession openWithCompletionHandler:^(FBSession *session, 
                FBSessionState status, 
                NSError *error) { 

地說:

FBSession.activeSession = session;