Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook4j API : Search

I'm using Facebook4j to get status with a 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 a exception probleme , the error is : java.lang.IllegalStateException: No Token available. ERROOOOOOR

like image 720
Mariama Mariama Avatar asked Dec 16 '22 08:12

Mariama Mariama


2 Answers

This is how you could use facebook4j without external configuration files. The code below provides 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...");
}
}

Note that it is important to FIRST make a call to "facebook.setOAuthAppId(..)" and THEN set the access token. Otherwise you'll get an IllegalStateException saying "OAuth app id/secret combination not supplied".

In this case, I've just used a default value for OAuthAppId.

like image 61
Niko Schenk Avatar answered Jan 01 '23 01:01

Niko Schenk


You forgot to set the access token with fac.setOAuthAccessToken("*****"). From the docs (emphasis mine):

All Graph API search queries require an access token passed in with the access_token=<token> parameter. The type of access token you need depends on the type of search you're executing.

  • Searches across page and place objects requires an app access token.
  • All other endpoints require a user access token.

You can generate one for yourself here, but remember that these access tokens have an expiration time.

like image 36
bfontaine Avatar answered Jan 01 '23 01:01

bfontaine