-1

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!

Labe
  • 1,262
  • 2
  • 20
  • 30

1 Answers1

0

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.

Hupfauer
  • 591
  • 1
  • 4
  • 15
  • Thank you for your answer. Is it possible to get the user token without asking to the client to log in with his account ? For example if I have a facebook account which is editor of all of my clients pages and i store username/password in my server properties ? – Labe Jan 22 '16 at 13:32
  • Kind off. You may extend the user token to a long lived token - see [here](https://developers.facebook.com/docs/facebook-login/access-tokens/expiration-and-extension#expiration-and-extension-of-access-tokens). Just test in the application against /me/permissions if you still have access with the long lived token, if not, just get a new one. If my answer (and or comment) helped you I would appreciate if you mark my answer as solution. – Hupfauer Jan 22 '16 at 14:51