How to get the details of the user using restfb.?

6.2k Views Asked by At

I am trying to get the lastname of my profile using restfb.But but each time the username is returned as null.I already have the acess token and permissions.I guess t some problem with the JSon object passing.how can i pass the json objects to a javabean and later retrieve it?

3

There are 3 best solutions below

2
AnAmuser On

This works for me:

FacebookClient facebookClient = new DefaultFacebookClient(MY_ACCESS_TOKEN);
User user = facebookClient.fetchObject("me", User.class);
out.println("Last name: " + user.getLastName());
0
Swamy On

Here is the code snippet

FacebookClient facebookClient = new DefaultFacebookClient("register a facebook application with required permissions, get that application token and paste here");

User user = facebookClient.fetchObject("me", User.class);

Connection<User> myFriends = facebookClient.fetchConnection("me/friends", User.class);

for(User friend:myFriends.getData())
{
    Connection<Page> myMovies = facebookClient.fetchConnection(friend.getId() + "/movies", Page.class);

    content = content + "Name: " + friend.getName();
    for(Page page:myMovies.getData())
    {
        content = content  + "\n" + "Movies: " + page.getName() + "\n";
    }
}

In this example your application needs "friends_likes" permission.

Hope this helps.

0
Ithar On

This happens when the access token you provided doesn't have the correct permissions to access the data. Best way to check this is by using the facebook graph API interface; noting the version. https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=me&version=v2.4

FacebookClient fbClient = new DefaultFacebookClient(accessToken, Version.VERSION_2_4);
User me = fbClient.fetchObject("me", User.class, Parameter.with("fields", "email,first_name,last_name,gender"));

Note: Your FB.login function must contain the correct scope for fields you want to access.

FB.login(function(response) {

           ...

        }, {scope: 'email'});