Facebook Graph API我/朋友什么都不返回

时间:2016-02-19 18:10:51

标签: javascript ios facebook cordova facebook-graph-api

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

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

// 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]。为什么我得到这个回复而不是朋友列表?

3 个答案:

答案 0 :(得分:1)

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

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

您也可以使用console.log

console.log(response);

只需在应用程序运行时将手机连接到计算机,然后使用chrome://inspect将其调试为网站(因为它就是这样)。

答案 1 :(得分:0)

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

function name() {
...
}

是的,您可以保留 var 关键字。另外,你应该试试这个:

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!");
        }
    });
}

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