Using Facebook API to get user’s info and his big picture in java

In this article I will show you the code for fetching user’s details using Facebook API. To proceed ahead you should be aware of the process of getting user’s access token. You can refer that article here :

Getting facebook access token of a user

Once we have the access token, we can use it to fetch user’s information. We will be using restfb java client for that.

1. Create facebook client

Here access_token is something you will have got after oauth flow for user login

FacebookClient client = new DefaultFacebookClient(access_token);

2. Fetch user info

Here ‘ids’ are comma separated ids of all users whose info you want to fetch. In the ‘fields’ we pass the response fields we are interested in. Right now we are interested in first name,gender and large profile picture of the user

JsonObject qq = facebookClient.fetchObject("/",
						JsonObject.class, Parameter.with("ids", ids),
						Parameter.with("fields",
								"gender,id,first_name,name,picture.type(large)"));

3. Iterate and extract data

Response will be an array. Now iterate over the array and extract fields for each user

	for (int y = 0; y < qq .length; y++) {
					JsonObject obj = qq.lgetJsonObject(y);
String fname = obj.getString("first_name");
String sex = obj.getString("gender");
String name = obj.getString("name");

4. Extracting picture

User’s picture is itself a complex object. It should be extracted like this

String pic = obj.getJsonObject("picture")
								.getJsonObject("data").getString("url");

Now we have basic data of the user. You can build any app on top of it

Mar Java Mit Java 🙂

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *