Post on Facebook pages via Graph API with Facebook4j

572 Views Asked by At

I want to create a tool wich allow a user to post his planning on several media at once : he has to fill a form with his establishment week planning, then I post it via newsletter, on his facebook and on his website.

I am struggling with the facebook part. I created an app and made the page subscribe to this app then I tried to use Facebook4j to post something on the page but I am not even able to get the page.

Here is my code :

Facebook facebook = new FacebookFactory().getInstance();
facebook.setOAuthAppId("{app_id}", "{app_secret}");
facebook.setOAuthPermissions("public_profile, manage_pages, publish_pages, publish_actions");
facebook.setOAuthAccessToken(new AccessToken("app_id|app_secret", null));
try {
        ResponseList<Account> accounts = facebook.getAccounts();
    } catch (FacebookException e) {
        e.printStackTrace();
    }

which always return me the error :

An active access token must be used to query information about the current user.

How can I have an active access token in order to post on pages which suscribed to my app?

NB : I am not sure I actually need an app. If there is an other way to post on multiple pages without asking for logging each time, I am ok with that too. (some kind of permanent page token maybe?)

Thanks!

1

There are 1 best solutions below

2
On

Okay first, yes you need an app to perform these requests.

To get what you describe you are requesting the permissions needed correctly, you still miss one - namely pages_show_list.

In addition you have to set the OAuthAccessToken to the users token not to the app token.