How to get all friends using Facebook v4

I am using Facebook v4.0.1 to integrate into my application. I am using FBSDKGraphRequest to get my friends list

FBSDKGraphRequest *friendsRequest = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me/friends" parameters:nil];
    FBSDKGraphRequestConnection *connection = [[[FBSDKGraphRequestConnection alloc] init] autorelease];
    [connection addRequest:friendsRequest
         completionHandler:^(FBSDKGraphRequestConnection *innerConnection, NSDictionary *result, NSError *error) {

         }];
    // start the actual request
    [connection start];

      

It just returns about 25 friends and an option called paging to get next friends.

How can I paging to get the next one or is there another way to get all friends once

+3


source to share


3 answers


"paging": {
     "previous": "me/friends?limit=25&before=NDMyNzQyODI3OTQw"
"next": "me/friends?limit=25&after=MTAxNTExOTQ1MjAwNzI5NDE="
}

      

The swap must contain "next" and "previous" URLs, and you will want to use those URLs instead of the original @ "me / friends" line, and you will get either the page before or after the current data. If there is no more "next", it means that you no longer need to request pages.



You can always try adding a limit parameter to your first query and see what happens when you exceed the 25 percent maximum. I'm not sure how much you can get away with getting at one time, but at least it's an upper limit, so it will return what it can if it runs out.

More paging information is available here: https://developers.facebook.com/docs/graph-api/using-graph-api/v2.3#paging

+1


source


The accepted answer got me headed in the right direction, but it's not 100% correct.

Request

For FacebookSDK 4.x, using graph v2.3, you will only get 25 results unless you go over your limit. The maximum limit you can choose is 1k. Here's an example of getting 100:

NSMutableString *facebookRequest = [NSMutableString new];
[facebookRequest appendString:@"/me/friends"];
[facebookRequest appendString:@"?limit=100"];

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                                  initWithGraphPath:facebookRequest
                                  parameters:nil
                                  HTTPMethod:@"GET"];
[request startWithCompletionHandler:<YOUR BLOCK HERE>];

      

Note. YOUR_BLOCK_HERE is the block you will use to process the results.

Paging

Now comes the question of paging. The JSON that was inserted in the accepted answer is incorrect. This is what the JSON looks like for / me / friends:

data =     (
            {
        id = 1234567890123456711;
        name = "John Snow";
    },
            {
        id = 1234567891234567891;
        name = "Ned Stark";
    }
);
paging =     {
    next = "https://graph.facebook.com/v2.3/<FB_USER_ID>/friends?limit=100&format=json&access_token=<SESSION_TOKEN>&offset=100&__after_id=enc_asdfasas2393nfaASDVASEqwe";
};
summary =     {
    "total_count" = 175;
};

      



We know from this JSON result from Facebook that we need to continue paging for the following two reasons: 1.) We have Graph results in the data part 2.) We got the following url.

Note. SESSION_TOKEN is the current session token that made the request.

Note. FB_USER_ID is the Facebook ID that made the request.

Now you are making a request to the next page. You must remove the Facebook url if you are going to use FBSDKGraphRequest.

NSString *nextUrl = [pagingDictionary objectForKey:@"next"];

//remove the 'https://graph.facebook.com/v2.3/'
nextUrl = [nextUrl substringFromIndex:32];

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                                              initWithGraphPath:nextUrl
                                              parameters:nil
                                              ];
[request startWithCompletionHandler:<YOUR_BLOCK_HERE>];

      

Advice

Write your code so that facebook swap is invoked with recursive block calls or recursive method calls. I have obviously simplified my post to focus on the question asked. This way you can write the block parsing code once and reuse it for different types of on-demand requests such as "me / friends" or "me / invitable_friends".

+15


source


You are trying to use this code

FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                               initWithGraphPath:@"/me/friends"
                                      parameters:params
                                      HTTPMethod:@"POST"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                      id result,
                                      NSError *error) {
    // Handle the result
}];

      

enter link here

-1


source







All Articles