4

I am trying to post on the wall of a facebook page. I am able to post on the user wall using App Access token.

I got the App Access Token through extending the DefaultFacebookClient

public class ConnectionService extends DefaultFacebookClient{

public ConnectionService(String appId, String appSecret) {
    AccessToken accessToken = this.obtainAppAccessToken(appId, appSecret);
    this.accessToken = accessToken.getAccessToken();
}
}

With this I am able to post to user wall using the appID and appSecret. But when I tried to post to Page Wall i get error of " The user hasn't authorized the application to perform this action"

Anyone can advice?

Alvin
  • 85
  • 1
  • 2
  • 5

4 Answers4

6

To post on a facebook page wall, you will need to follow these steps:

  1. Head over to https://developers.facebook.com/tools/explorer
  2. Click on "Get Access Token"
  3. Under "Extended Permissions" tab, select select manage_pages and publish_actions and hit "Get Access Token"
  4. Now under Graph API, under Get call, type in "me/accounts" and hit Submit
  5. In the screen below, you will see the "data" json object with all your pages and page access tokens.
  6. Grab the desired page token access and replace the PAGE_ACCESS_TOKEN in the code below with this token.
  7. Replace PAGE_NAME with your page name (the page name slug in the URL).
  8. Run the below code and that should do the job :)

final FacebookClient fb = new DefaultFacebookClient(PAGE_ACCESS_TOKEN); final Page page = facebookClient.fetchObject(PAGE_NAME, Page.class); facebookClient.publish("PAGE_NAME/feed", FacebookType.class, Parameter.with("message", "RestFB test"));

sufinawaz
  • 3,623
  • 1
  • 25
  • 24
  • 1
    Thanks for the help! Your code works, with a small correction: The PAGE_NAME is really the page id, the numerical field you find at the bottom of the GET request. Not the name itself. – Dimitris Sfounis Dec 30 '14 at 14:48
  • Actually PAGE_NAME is the slug of the page name in the url that you see. It worked with that identifier for me. Updated Answer to reflect that. Thanks for pointing out :) – sufinawaz Jan 22 '15 at 15:27
  • Thanks a lot, your solution works perfect. But there is a problem, it's posted by Graph API Explorer, can this be changed? so all can see this posts. – Denees Feb 21 '15 at 13:09
  • You're welcome. In the example I posted, the posts were made as the page and everybody could see them. – sufinawaz Feb 25 '15 at 13:31
  • does it still work? it seems that facebook removed publish_actions – iirekm Mar 03 '19 at 10:47
1

The App Access Token is the most basic one, and will not allow you to post anything. In order to post something to a Facebook Page (as a Page), you need to get a Page Access Token.

The process is a bit complicated, because you actually need to authorize the user with the "manage_pages" permission first, with the User Access Token you can call the API to get a Page Access Token (/me/accounts).

See those links:

Btw, the REST API is deprecated: https://developers.facebook.com/blog/post/616/

You can also try the "Client Token" (Developer Settings > Advanced), i never worked with that one but it could be the easiest solution. In any case, an App Access Token is the wrong one.

andyrandy
  • 72,880
  • 8
  • 113
  • 130
0

Make sure that the scopes you mentioned while authenticating user includes manage_pages also. This error occurs when you have not included this in your scope. Refer this

Jhanvi
  • 5,069
  • 8
  • 32
  • 41
0

Since u r generating access token from java class. u can set the permissions u require from user in ur manage app link from ur facebook profile page and get the access token here.....

Bharath R
  • 1,491
  • 1
  • 11
  • 23
  • also, please use english instead of "u r" and "u" slang. does not really take more time for you, but it´s a lot easier to read. – andyrandy Oct 21 '13 at 11:00