user3715098
user3715098

Reputation: 73

How to get facebook id of friends

I want to get facebookid of my friends. But seems like its sending different id's

The situation is i have a friends name "abc" when i loads the friends list then i am getting following response for friend "abc" from facebook

<__NSCFArray 0xce76290>(
{
    id = 668201987254085;
    name = "abc";
}
)

and my facebook id is "123764734754"returned by facebook

i am using below code for this

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
SLRequest *friendsListRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                   requestMethod:SLRequestMethodGET
                                                              URL:URLIFY(@"https://graph.facebook.com/me/friends")
                                                      parameters:nil];
friendsListRequest.account = facebookAccount;
[friendsListRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
    if (responseData) {
        NSLog(@"Got a response: %@", [[NSString alloc] initWithData:responseData
                                                           encoding:NSUTF8StringEncoding]);
        if (urlResponse.statusCode >= 200 && urlResponse.statusCode < 300) {
            NSError *jsonError = nil;
            NSDictionary *friendsListData = [NSJSONSerialization JSONObjectWithData:responseData
                                                                            options:NSJSONReadingAllowFragments
                                                                              error:&jsonError];
            if (jsonError) {
                NSLog(@"Error parsing friends list: %@", jsonError);
            } else {
                self.userList = friendsListData[@"data"];
            }
        } else {
            NSLog(@"HTTP %ld returned", (long)urlResponse.statusCode);
        }
    } else {
        NSLog(@"ERROR Connecting");
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    });
}];

But when i am logging in using user name "abc" then for abc (current login user) i am getting different id say "10008772786278"

same thing when i login then i got my facebook id"123764734754" but when my friend "abc" login then in his friends list response they got my id "897349579554"

why this thing happen? how to get same facebook id of the user that is returned after when he login

Upvotes: 0

Views: 320

Answers (2)

Pitambar Indoria
Pitambar Indoria

Reputation: 380

Please Try this code.

[FBSession openActiveSessionWithReadPermissions:@[@"basic_info"] allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState state, NSError *error){
        NSLog(@"Error :%@",error);
        if (session.isOpen && !error) {

            [FBRequestConnection startWithGraphPath:@"me/friends" parameters:nil HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection,id result, NSError *error){

                if (!error) {
                    NSLog(@"result %@",result);
                    [array_firendList addObjectsFromArray:[result valueForKey:@"data"]];

                    NSLog(@"count %ld",(unsigned long)[array_firendList count]);
                }
 }];

Upvotes: 6

andyrandy
andyrandy

Reputation: 73984

Facebook recently changed a lot of stuff, see here: https://developers.facebook.com/docs/apps/changelog

See "App-scoped User IDs", you don´t get the real IDs anymore:

To better protect people's information, when people log into a version of your app that has been upgraded to use Graph API v2.0, Facebook will now issue an app-scoped ID rather than that person's orginal ID

Also important:

The /me/friends endpoint no longer includes the full list of a person's friends. Instead, it now returns the list of that person's friends who are also using your app.

To make sure you always get an App Scoped ID, use /v2.0/{object} instead of /{object}. For example, to get the ID of the logged in user you can use /v2.0/me, for getting the friend list you can use /v2.0/me/friends. This is only necessary if your App is created before May 2014, if you created it later it will only use v2.0 anyway.

Another hint: make sure you get the IDs (with /me or /me/friends) on the same platform and of course with the same App, don´t compare the IDs you get with the Graph API Explorer (for example) with the ones you get in the App.

Upvotes: 1

Related Questions