3

I am developing a photo viewer, which is a java desktop application and to integrate it with facebook I am using the Facebook4J library. But I am clueless as to where to provide the username and password for the user....

I am using the following code :

public static  Configuration createConfiguration() {
    ConfigurationBuilder confBuilder = new ConfigurationBuilder();

    confBuilder.setDebugEnabled(true);
    confBuilder.setOAuthAppId("************");
    confBuilder.setOAuthAppSecret("***************");
    confBuilder.setUseSSL(true);
    confBuilder.setJSONStoreEnabled(true);
    Configuration configuration = confBuilder.build();
    return configuration;
}
public static void main(String[] argv) throws FacebookException {
    Configuration configuration =  createConfiguration();
    FacebookFactory facebookFactory = new FacebookFactory(configuration );
    Facebook facebookClient = facebookFactory.getInstance();
    AccessToken accessToken = null;
    try{
        OAuthSupport oAuthSupport = new OAuthAuthorization(configuration );
        accessToken = oAuthSupport.getOAuthAppAccessToken();

    }catch (FacebookException e) {
        System.err.println("Error while creating access token " + e.getLocalizedMessage());
    }
    facebookClient.setOAuthAccessToken(accessToken);
//results in an error says {An active access token must be used to query information about the current user}
    facebookClient.postStatusMessage("Hello World from Facebook4J."); 
}

How to pass the username and password the user?

Tamojit Chatterjee
  • 161
  • 1
  • 1
  • 10
  • Hi May I know how it was resolved means how the credentials supplied for login – RamBen Nov 19 '15 at 08:52
  • you can use the browser provided by javafx and take the user credentials from there. and also get the acces token from the temporary link to which the browser is redirected for a short period of time. have a look at my git hub project which already does this... https://github.com/tamojit9/java-facebook-api – Tamojit Chatterjee Nov 20 '15 at 17:12
  • Thank you Tamojit Chatterjee, Just I checked your github code here I need login with out any login forms means automated login/credentials supply – RamBen Nov 23 '15 at 06:03

1 Answers1

4

You should retrieve an OAuthAppId from facebook. To do this, create an App on Facebook Developers and click "Create new app". Fill in the form, then copy paste the AppId and Appsecret from the page that appears in your code. Example:

confBuilder.setOAuthAppId("YOUR APPID HERE");
confBuilder.setOAuthAppSecret("YOUR APPSECRET HERE");

To get the AccessToken, you have to build a login flow as described here

k1ps
  • 79
  • 6
  • i have already built an app and have the app-id and appsecret but did not reveal it here for obvious reasons. I am having a problem designing the login flow – Tamojit Chatterjee Dec 22 '13 at 19:02
  • You have to send a HTTPS request to facebook.com: GET https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials (leave client_credentials as it is). – k1ps Dec 22 '13 at 20:14
  • hi k1ps using the above link we will get app-accesstoken, if we need user accesstoken with out explicit login how we can get. means need to get the user acesstoken programatically (may using the fb login automatically) – RamBen Nov 19 '15 at 09:40