2016-02-19 53 views
1

我有一个应用程序与Facebook登录和登录和获取基本的个人资料信息工作正常。但是,当我尝试获取Facebook朋友(只返回也使用相同应用的朋友)时,我从Facebook API获取[object Object]。我有权设置好朋友(根据我的应用的Facebook开发者页面)。Facebook图形API我/朋友没有返回

我的代码看起来像这样(我使用PhoneGap的插件,但该代码是类似于JS版Facebook的API):

// Login function (permissions included) 
var login = function() { 
    if (!window.cordova) { 
     var appId = prompt("123456789101112", ""); 
    } 
    facebookConnectPlugin.login(["email, user_friends"], 
     // SUCCESS 
     function (response) { 
      alert('Login successful!'); 
     }, 
     // FAILURE 
     function (response) { 
      alert('Login failed') 
     } 
    ); 
} 

// Get the friends 
var getFacebookFriends = function() { 
    facebookConnectPlugin.api("me/friends", 
    // FAILURE 
    function(response) { 
     alert('Retrieving Facebook friends failed'); 
    }, 
    // SUCCESS 
    function(response) { 
     alert(JSON.stringify('Facebook friends: ' + response)); 
    }); 
} 

警报说Facebook friends: [object Object]。我确信我有一位朋友也使用相同的权限登录到应用程序。他没有出现在名单上,只有空的[object Object]。为什么我会得到这个回应,而不是朋友的名单?

回答

1

它不是空的,它是一个JSON对象。你只需要它正确地解码:

alert('Facebook friends: ' + JSON.stringify(response)); 

您也可以只使用console.log

console.log(response); 

只需将手机连接到电脑上,而你的应用程序正在运行,并使用chrome://inspect调试它像一个网站(因为那就是它)。

0

如果要定义一个函数,你可以这样做:

function name() { 
... 
} 

是的,你可以留下变种关键字。另外,你应该试试这个:

function getFacebookFriends() { 
    facebookConnectPlugin.api('/me/friends', function(response) { 
     if(response.data) { 
      $.each(response.data,function(index,friend) { 
       alert(friend.name + ' - FB ID:' + friend.id); 
      }); 
     } else { 
      alert("Unable to return Facebook friends!"); 
     } 
    }); 
} 
+0

不知道你为什么提到var关键字(在这种情况下它并不重要),但我不会为这个简单的任务使用jquery ... – luschn

-1
I just Hope if this could help you !! 

[FBSDKProfile enableUpdatesOnAccessTokenChange:YES]; in - (void)viewDidLoad Method 

- (IBAction)Loginwithfacebookaction:(id)sender 

{ 
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; 
    [login logOut]; 
    [login logInWithReadPermissions:@[@"public_profile", @"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) { 

     if (error) 
     { 

      // There is an error here. 

     } 
     else 
     { 
      if(result.token) // This means if There is current access token. 
      { 
       // Token created successfully and you are ready to get profile info 
       [self Method]; 
      } 
     } 
    }]; 
    } 

-(void)Method { 

    FBSDKGraphRequest *requestMe = [[FBSDKGraphRequest alloc]initWithGraphPath:@"/me?fields=first_name, last_name, picture, email" parameters:nil]; 

    FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init]; 

    [connection addRequest:requestMe completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { 

     if(result) 
     { 
      if ([result objectForKey:@"email"]) { 
       email = [result objectForKey:@"email"]; 

      NSLog(@"Email: %@",[result objectForKey:@"email"]); 
       [[NSUserDefaults standardUserDefaults] setObject:email forKey:@"useremail"]; 

      } 
      if ([result objectForKey:@"first_name"]) { 

       NSLog(@"First Name : %@",[result objectForKey:@"first_name"]); 
       name = [result objectForKey:@"first_name"]; 
       [[NSUserDefaults standardUserDefaults] setObject:name forKey:@"userNameLogin"]; 
      } 
      if ([result objectForKey:@"id"]) 
      { 
      NSLog(@"User id : %@",[result objectForKey:@"id"]); 

      } 
        } 


    }]; 
    [connection start]; 
} 
相关问题