0

I wrote something like this, in JavaFX.

public class ShareOn {
    private static String appId = "", appSecret = "", accessToken="";
    private static Facebook facebook = null;
    private static void createFbConnection()
    {
    facebook = new FacebookFactory().getInstance();
    facebook.setOAuthAppId(appId, appSecret);       
    AccessToken at = new AccessToken(accessToken);
    facebook.setOAuthAccessToken(at);
}
public static void shareOnFacebook()
{
    createFbConnection();
    try{
        facebook.postStatusMessage("Hello World from Facebook4J.");
    }catch(Exception ex){
        ex.getMessage();
    }
}

I created a new app on Facebook for developers. From there i have the "appId", "appSecret" . Could you tell me where it's the mistake ?

1 Answers1

0

What's (probably) going wrong

I don't now the Facebook API at all, but this line looks weird:

AccessToken at = new AccessToken(accessToken);

where you previously set accessToken="". That's unlikely to work.

The access token should be an ephemeral thing which you should get via an API call. It is not the same as appId and appSecret, which aren't going to change. You aren't making the API call to retrieve a new access token from Facebook (that's why nothing happens - you don't do anything).

What you (probably) should do

You should get a valid access token from Facebook, likely via a Facebook API call of some kind.

What you need to do depends upon the kind of access token are you trying to get. Looking at facebook4j doc, it seems Facebook has different kinds: User access, app access, page access and device access.

If it is user access, then you will likely need to run some kind of embedded browser (perhaps a web view and perhaps with some server side code to serve the content in web view and integrate with Facebook) to allow the user to authenticate with Facebook to get an access token for their account. Getting user access permission will likely be very tricky as the facebook4j provided sample does this via a JSP driven server based web application rather than a java client application like you have with JavaFX.

If it is app access, things should be considerably easier, it would seem there is an API for that, which (hopefully) doesn't require you to pre-authenticate a user:

facebook.getOAuthAppAccessToken();

jewelsea
  • 150,031
  • 14
  • 366
  • 406