Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post to Facebook Page wall using RestFB api

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?

like image 798
Alvin Avatar asked Mar 21 '23 23:03

Alvin


1 Answers

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"));

like image 73
sufinawaz Avatar answered Apr 24 '23 21:04

sufinawaz