Facebook4j API: Search

I use Facebook4j to get status with keyword

facebook4j.conf.ConfigurationBuilder fac = new facebook4j.conf.ConfigurationBuilder(); fac.setDebugEnabled(true) .setOAuthAppId("******") .setOAuthAppSecret("********") .setOAuthPermissions("email,publish_stream,..."); FacebookFactory ff = new FacebookFactory(fac.build()); facebook = ff.getInstance(); new Thread(new Runnable() { @Override public void run() { try { search(); } catch (Exception e) { // TODO: handle exception System.out.println(e +" ERROOOOOOR"); }}}).start(); } //search public void search() throws Exception { ResponseList<JSONObject> results = facebook.search("%23morocco"); System.out.println(results); for (JSONObject result : results) { System.out.println(result); } results = facebook.search("orange", new Reading().until("yesterday")); System.out.println(results); for (JSONObject result : results) { System.out.println(result); } } 

I replaced * with facebook api key I have an exception problem, error: java.lang.IllegalStateException: no tokens are available. ERROOOOOOR

+4
source share
2 answers

You forgot to set the access token using fac.setOAuthAccessToken("*****") . From the docs (my attention):

All Graph API search requests require an access token passed with the parameter access_token=<token> . The type of access token you need depends on the type of search you are performing.

  • To search for page and place objects, an application access token is required.
  • All other endpoints require a user access token .

You can create for yourself here , but remember that these access tokens have an expiration date.

+4
source

So you can use facebook4j without external configuration files. The code below gives a minimal example. Here is my simple demo:

 import facebook4j.Facebook; import facebook4j.FacebookException; import facebook4j.FacebookFactory; import facebook4j.auth.AccessToken; public class Facebook4JMinimalExample { /** * A simple Facebook4J client. * * * @param args * @throws FacebookException */ public static void main(String[] args) throws FacebookException { // Generate facebook instance. Facebook facebook = new FacebookFactory().getInstance(); // Use default values for oauth app id. facebook.setOAuthAppId("", ""); // Get an access token from: // https://developers.facebook.com/tools/explorer // Copy and paste it below. String accessTokenString = "PASTE_YOUR_ACCESS_TOKEN_STRING_HERE"; AccessToken at = new AccessToken(accessTokenString); // Set access token. facebook.setOAuthAccessToken(at); // We're done. // Write some stuff to your wall. facebook.postStatusMessage("Wow, it works..."); } } 

Please note that it is important that FIRST makes a call to "facebook.setOAuthAppId (..)" and THEN sets the access token. Otherwise, you will get an IllegalStateException in which "OAuth app id / secret combination is not provided."

In this case, I just used the default value for OAuthAppId.

+8
source

Source: https://habr.com/ru/post/1494638/


All Articles