1

I need your help please.

I'm developing a java application using restfb library, in which i want to get all the facebook users who liked my fan page on fb. I worked for many days to do that but i didn't find a solution.

I wrote this code but it return the number of likers only:

FacebookClient fb= new DefaultFacebookClient("ACCESS_TOKEN");
        Page page = fb.fetchObject("PAGE_ID", Page.class);
        System.out.println("My pages likes: " + page.getLikes());

Note that i'm getting the ACCESS_TOKEN from the Graph API Explorer when i'm logged in on the fb page, because i didn't find a way to get the token from java code. So when i logout i sea this error:

Exception in thread "main" com.restfb.exception.FacebookOAuthException: Received Facebook error response of type OAuthException: Error validating access token: The session is invalid because the user logged out.
    at com.restfb.DefaultFacebookClient$DefaultGraphFacebookExceptionMapper.exceptionForTypeAndMessage(DefaultFacebookClient.java:766)
    at com.restfb.DefaultFacebookClient.throwFacebookResponseStatusExceptionIfNecessary(DefaultFacebookClient.java:688)
    at com.restfb.DefaultFacebookClient.makeRequestAndProcessResponse(DefaultFacebookClient.java:630)
    at com.restfb.DefaultFacebookClient.makeRequest(DefaultFacebookClient.java:592)
    at com.restfb.DefaultFacebookClient.makeRequest(DefaultFacebookClient.java:556)
    at com.restfb.DefaultFacebookClient.fetchObject(DefaultFacebookClient.java:244)
    at testrestfb.testfb.main(testfb.java:95)

Please help me to solve this problem..

Thanks in advance.

Richie Hughes
  • 130
  • 10
kabalan
  • 13
  • 4
  • 1
    could u better format our code with clarity and since ques is also related to facebook better would be add that tag as well – exexzian Nov 07 '13 at 11:56
  • I think its not duplicate as user asked about restfb which is in java and above question is in PHP, so solving problem is different for new users.. – Imran Nov 07 '13 at 12:33

1 Answers1

0

You should get the information about such thing using fql query.

Please see below of Making a More Complex Call

Here we make the API call fql.query to execute a custom query.

// FQL query which asks Facebook for your friends' names,
// profile picture URLs, and network affiliations

String query =
  "SELECT name, pic_big, affiliations FROM user " +
  "WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=12345)";

// Executes an API call with result mapped to a list of classes we've defined.
// Note that you can pass in an arbitrary number of Parameters - here we
// send along the query as well as the "give me HTTPS URLs" flag

List<User> users =
  facebookClient.executeForList("fql.query", sessionKey, User.class,
    Parameter.with("query", query), Parameter.with("return_ssl_resources", "true"));

Details are here

Imran
  • 5,376
  • 2
  • 26
  • 45
  • thanks Imran for your reply, i tried your solution but it still require a valid access token when creating an instance of FacebookClient. So when i'm logged out from the fb page i receive the same error. Are there any method to generate valid access token in java? – kabalan Nov 08 '13 at 09:03
  • This will help you http://stackoverflow.com/questions/13671694/restfb-using-a-facebook-app-to-get-the-users-access-token – Imran Nov 08 '13 at 11:55
  • You can mark it as correct answer, to help others.. :) – Imran Nov 27 '13 at 09:16
  • @Imran it shows error for session key....http://stackoverflow.com/questions/22187977/error-when-using-fql-query.. – lulu Mar 05 '14 at 03:35