Retrieving Friends' Username on Facebook Graph API Using FBGraphUser Class

Retrieve Friends’ Username

In this article, we will explore how to retrieve the username of your Facebook friends using the FBGraphUser class. We’ll delve into the code snippet provided in the Stack Overflow question and explain why the username property is null.

Understanding FBGraphUser

The FBGraphUser class represents a user on Facebook Graph API. It provides access to various attributes of a user, such as their name, email, and username. In this article, we’ll focus specifically on retrieving the username.

Retrieving Friends’ Username

To retrieve the username of your friends, you can use the FBGraphUser class in combination with Facebook Graph API’s endpoint for retrieving users by ID.

Why is friend.id working?

In the provided code snippet, the author mentions that friend.id works right. This is because friend.id returns the unique identifier of the selected friend, which can be used to retrieve their user information from Facebook Graph API.

To demonstrate this, let’s create a new method in your view controller that retrieves the friends’ users by ID:

- (void)getFriendsUsername {
    for (id<FBGraphUser> friend in self.friendPickerController.selection) {
        NSLog(@"%@", friend.id);
        
        // Create a request URL to retrieve the user information
        NSURL *requestURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@?fields=id,username&access_token=%@", friend.id, self.accessToken]];
        
        // Send a GET request to Facebook Graph API
        NSURLRequest *request = [NSURLRequest requestWithURL:requestURL];
        NSHTTPURLResponse *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        
        if (response.statusCode == 200) {
            NSString *jsonResponse = [response bodyDataUsingEncoding:NSUTF8StringEncoding];
            
            // Parse the JSON response to extract the username
            NSDictionary *userData = [JSONSerializer dictionaryFromJSONString jsonstring:jsonResponse];
            NSLog(@"%@", userData[@"username"]);
        }
    }
}

In this example, we create a new method called getFriendsUsername that retrieves the friends’ users by ID. We use the NSURL class to construct the request URL and send a GET request to Facebook Graph API using the NSURLConnection class.

We then parse the JSON response to extract the username using a third-party library like JSONSerializer. Note that we also log the friend.id value to verify that it’s working correctly.

The code snippet provided in the Stack Overflow question demonstrates how to retrieve the friends’ names using the FBGraphUser class, but unfortunately, the username property is null. By following our steps above, you can modify this code snippet to retrieve the username of your Facebook friends.

Best Practices for Retrieving Friends’ Username

When retrieving the username of your Facebook friends, keep in mind the following best practices:

  • Use the FBGraphUser class to access user attributes.
  • Construct a request URL using the Facebook Graph API’s endpoint for retrieving users by ID.
  • Send a GET request to Facebook Graph API and parse the JSON response to extract the desired information.
  • Always handle errors and edge cases when working with external APIs.

Conclusion

In this article, we explored how to retrieve the username of your Facebook friends using the FBGraphUser class. We delved into the code snippet provided in the Stack Overflow question and explained why the username property is null. By following our steps above, you can modify this code snippet to retrieve the username of your Facebook friends.

Additionally, we discussed best practices for retrieving friends’ username, such as using the FBGraphUser class, constructing a request URL correctly, sending a GET request, and parsing the JSON response.

By mastering these techniques, you’ll be able to build robust applications that seamlessly interact with Facebook Graph API.


Last modified on 2023-09-10