7

FBGraphUser object doesn't have the property friends or user_friends.

If I print the FBGraphUser, this is what I get:

{
email = "tksybnk_goldmanstein_1414083483@tfbnw.net";
"first_name" = Maria;
gender = female;
id = 1515324765375152;
"last_name" = Goldmanstein;
link = "https://www.facebook.com/app_scoped_user_id/1515324765375152/";
locale = "en_US";
"middle_name" = Amfibibiijdh;
name = "Maria Amfibibiijdh Goldmanstein";
timezone = 0;
"updated_time" = "2014-10-23T16:58:11+0000";
verified = 0;
}

The permissions I request are:

self.fbLoginView.readPermissions = ["public_profile", "email", "user_friends"]

When I do the request in the Graph API Explorer, I do get the proper response:

{
  "data": [
    {
      "name": "Open Graph Test User", 
      "id": "289719481225025"
    }
  ], 
  "paging": {
    "next": "https://graph.facebook.com/v2.1/1515324765375152/friends?limit=5000&offset=5000&__after_id=enc_AezwfD79EK96eREIyB6ZR8fvDu4G4q_wRb49JG4FYp_kNnWK2DNcdLt_LMnDodVraWMBXVR89Cw0FcTYMPmYSt1Q"
  }, 
  "summary": {
    "total_count": 1
  }
}

Is there a special way to access the friends, or should they be printed in this dictionary?

user83039
  • 3,047
  • 5
  • 19
  • 31
  • 1
    The last part seems to be a request for /me/friends. The first part doesn't look like a request to /me/friends. You are really doing the same exact request in both cases? – WizKid Oct 23 '14 at 23:16
  • No, the first part isn't a request for that. The Facebook SDK just provides that data after it's authorized. I guess I have to do another request to get the friends? I figured I didn't, since the first 2 permissions were automatically bundled in the FBGraphUser. – user83039 Oct 23 '14 at 23:26

3 Answers3

6

Here is what I ended up doing. Unlike the first 2 permissions, I had to make a separate request to get the friends:

FBRequestConnection.startForMyFriendsWithCompletionHandler({ (connection, result, error: NSError!) -> Void in
        if error == nil {
            var friendObjects = result["data"] as [NSDictionary]
            for friendObject in friendObjects {
                println(friendObject["id"] as NSString)
            }
            println("\(friendObjects.count)")
        } else {
            println("Error requesting friends list form facebook")
            println("\(error)")
        }
    })
user83039
  • 3,047
  • 5
  • 19
  • 31
2

Get Friends list using below code.

var friendsRequest : FBRequest = FBRequest.requestForMyFriends()
friendsRequest.startWithCompletionHandler
{
    (connection:FBRequestConnection!,   result:AnyObject!, error:NSError!) -> Void in
    var resultdict = result as NSDictionary
    println("Result Dict: \(resultdict)")
    var data : NSArray = resultdict.objectForKey("data") as NSArray

    for i in 0 ..< data.count 
    {
      let valueDict : NSDictionary = data[i] as NSDictionary
      let id = valueDict.objectForKey("id") as String
      println("the id value is \(id)")
    }

    var friends = resultdict.objectForKey("data") as NSArray
    println("Found \(friends.count) friends")
}
Vikram Pote
  • 5,433
  • 4
  • 33
  • 37
  • Used this code, able to see total_count but no the actual list. What am I doing wrong?: http://stackoverflow.com/questions/29466473/how-to-get-list-of-all-facebook-friends-when-using-parse – user1406716 Apr 06 '15 at 06:39
  • Yes, i'm also getting this issue. – Ritesh Kumar Singh Apr 20 '15 at 11:04
  • same for me.. it seems the max return of actual friends data are only ranging to 10-25 items and can't fetch the second one even though there is the next link it gives nothing but a go back link – eNeF Oct 09 '15 at 02:56
  • Here in friends.count you only get that user's count who made login to your app. – Vikram Pote Oct 09 '15 at 03:43
0

Get the ID of friends using the app, you can create test users and friends to test at https://developers.facebook.com/apps

let _graphRequest = GraphRequest(graphPath: "me/friends", parameters: ["fields": "id"])
        _graphRequest.start(completionHandler: { (connection, result, error) in
            if error != nil {
                print("Error fb", error!.localizedDescription)
            }

            else {
                
                let field = result! as? [String: Any]
                var friendObjects = field!["data"] as! [NSDictionary]
                for friendObject in friendObjects {
                    print("found a friend:  \(friendObject["id"] as! NSString)")
                }
                
    
            }
        })