0

i want to fetch Facebook Mutual friends in my ios App

this is my try to get mutual friends i call with this Link

[NSString stringWithFormat:@"me/mutual_friends/%@?fields=installed",fbuserId]

it give for me Error code 5 and when i use @"me/friends" it returns result i also try using graph api explorer it gives unknown path

APi Graph Explorer try

ALso my app have user_friends Permission can any one tell me the correct path ?

khaled
  • 865
  • 9
  • 24
  • https://developers.facebook.com/docs/graph-api/reference/v2.1/user.context/mutual_friends – CBroe Aug 09 '14 at 22:56
  • @CBroe can you modify my Link i do not know how to put context me/mutual_friends/%@?fields=installed" – khaled Aug 09 '14 at 23:10
  • Using this with `/me` doesn’t make much sense – you want to find the mutual friends between your current app user and another user, right? Then you need to use the (app-scoped) user id of that other user. – CBroe Aug 09 '14 at 23:14
  • @CBroe i also trying this but no result [NSString stringWithFormat:@"%@/mutual_friends/%@?fields=installed",MyFbId,fbuserId] – khaled Aug 09 '14 at 23:17
  • Don’t invent you own “syntax”, but follow the one given in the documentation. – CBroe Aug 09 '14 at 23:23
  • it works in v 1.0 but now it does not work i want to fetch mutual friends in that way is it possible in another way can you give me link in Graph APi Explorer fetching the mutual friends between me and another user – khaled Aug 09 '14 at 23:28
  • In API v2.0 you can only get friends that are also using the app. – CBroe Aug 09 '14 at 23:29
  • @CBroe ok Can you Tell Me how can i fetch Mutual Friends in Graph Api Explorer can you send me the Link which fetch the Mutual Friends only between two Profile Ids ? – khaled Aug 10 '14 at 04:27

2 Answers2

2

You're using the wrong endpoint syntax, as @CBroe already pointed out, and which is also clear from the docs.

You need to use

/{user-id}.context/mutual_friends

The {user-id} is the user you're interested in, and the "other" user is the one which Access Token was used for the request. This ONLY works for users which are both already users of your app.

Tobi
  • 31,405
  • 8
  • 58
  • 90
  • did you mean for example like this /1310347645.724379967/mutual_friends i use it and give to me Unknown path components i will be thankful if you give me example – khaled Aug 10 '14 at 11:47
  • I described everything. How hard can this be? And why Do you remove parts which are neccessary? – Tobi Aug 10 '14 at 12:10
  • i am really sorry but i am beginner in ios and Using FaceBook SDK i use what you see but it gives to me Unknown path components kindly can you give example – khaled Aug 10 '14 at 12:26
  • i make exactly what you said but it gives Unknown path components – khaled Aug 10 '14 at 13:05
  • @CBroe if you do not believe me can you try it on this Link https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=552779567.context%2Fmutual_friends&version=v2.0 – khaled Aug 10 '14 at 13:14
  • That's because you're using a global user_id, which is not allowed by Graph API v2.0. Use an app-scoped user_id and everything is fine. – Tobi Aug 10 '14 at 13:21
  • The manual even has examples … `userid?fields=context.fields(mutual_friends)` – CBroe Aug 10 '14 at 13:34
  • I think then we can't help you. I tested this a few moments ago, and it works. – Tobi Aug 10 '14 at 13:47
  • ok can you Give me the complete Example you test and works with you the complete one – khaled Aug 10 '14 at 13:50
  • No, I can't, because when using app-scoped user_ids, it only works with the respective app. – Tobi Aug 10 '14 at 14:40
0

Yes, quite easy solution i have for you.

NSString *myFriendId = [NSString stringWithFormat:@"/%@",#YourFriendId#];


    NSString *hashedAccessToken = [self hmac:accessToken withKey:kAPP_SECRETE];
    NSDictionary *params = @{
                             @"fields" : @"context.fields(mutual_friends)",
                             @"appsecret_proof" : hashedAccessToken,
                             @"access_token" : accessToken
                             };


    /* make the API call */
    [FBRequestConnection startWithGraphPath:myFriendId parameters:params  HTTPMethod:@"GET" completionHandler:^
     (
      FBRequestConnection *connection, id result, NSError *error
      ){//Handle Respons;}];

And here is the hmac function (Add #import )

//SHA256 HASHMAC
- (NSString *)hmac:(NSString *)access_token withKey:(NSString *)app_secret
{
    const char *cKey  = [app_secret cStringUsingEncoding:NSASCIIStringEncoding];
    const char *cData = [access_token cStringUsingEncoding:NSASCIIStringEncoding];
    unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
    CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
    NSData *HMACData = [NSData dataWithBytes:cHMAC length:sizeof(cHMAC)];
    const unsigned char *buffer = (const unsigned char *)[HMACData bytes];
    NSMutableString *HMAC = [NSMutableString stringWithCapacity:HMACData.length * 2];
    for (int i = 0; i < HMACData.length; ++i){
        [HMAC appendFormat:@"%02x", buffer[i]];
    }
    return HMAC;
}
Florian Schaetz
  • 10,454
  • 5
  • 32
  • 58