2

I am using facebook4j i have set the configuartion details in facebook4j.properties file. But when i trying to get the accesstoken it shows

SEVERE: Error while creating the Access TokenOAuth app id/secret combination not supplied
java.lang.IllegalStateException: OAuth app id/secret combination not supplied
    at facebook4j.FacebookBaseImpl.getOAuth(FacebookBaseImpl.java:247)
    at facebook4j.FacebookBaseImpl.getOAuthAuthorizationURL(FacebookBaseImpl.java:213)
    at facebook4j.FacebookBaseImpl.getOAuthAuthorizationURL(FacebookBaseImpl.java:206)

Could anyone can provide a example for the facebook4j for java console application

Facebook facebookClient = new FacebookFactory().getInstance();
        return facebookClient;
user2196474
  • 319
  • 1
  • 5
  • 15
  • please provide your code that is not working so we can get an idea what's wrong with it – Marco Forberg Jun 03 '13 at 12:27
  • How can i add access token using OAUTH – user2196474 Jun 03 '13 at 12:33
  • How do u get the access token value – user2196474 Jun 03 '13 at 12:35
  • i think the general meaning of this answer http://stackoverflow.com/questions/16895667/need-sample-code-to-retrieve-the-contacts-from-google-api#16896408 applied to you too – Marco Forberg Jun 03 '13 at 12:37
  • @MarcoForberg i accept but when i accesss the user details it shows error `SEVERE: Error while getting the facebook users {"error":{"message":"A user access token is required to request this resource.","type":"OAuthException","code":102}} FacebookException [statusCode=400, response=HttpResponse{statusCode=400, responseAsString='{"error":{"message":"A user access token is required to request this resource.","type":"OAuthException","code":102}} ', is=sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@2c766.java:65) ` – user2196474 Jun 03 '13 at 13:17

1 Answers1

7

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.

Niko Schenk
  • 271
  • 3
  • 7
  • what should I set in OAuthAooId and what is access Tokens?How I can get it? – Muneeb Nasir Mar 15 '15 at 12:39
  • 1
    You don't have to set anything for OAuthAppId in this particular example. Getting the access token requires you to log in into your FB account and access this URL: https://developers.facebook.com/tools/explorer. Just copy the access token string and paste it into the code. – Niko Schenk Apr 08 '15 at 09:41