Post Status on Facebook Timeline using restfb Graph API in ADF & Java

Sharing is Caring

Hello All

The previous post was about generating a temporary access token and using it get Facebook profile detail using restfb Graph API, Go through the previous post before this as graph API basics are described in that post

Now In this post, I am talking about posting Facebook status update so for this, I am extending the same sample application

There are two types of Facebook status, first is simple text status and the second one is with text with some website URL. 

So Added two new af:inputText (for text status and URL) and a button to post the status on facebook timeline

Next step is to get access token with a new permission publish_actions to post to facebook

Allow Graph API to post on facebook for you

See Managed Bean Code

Component Bindings in Managed Bean-

 

//Component binding for status inputText
private RichInputText statusBind;
//Component binding for website url inputText
private RichInputText linkBind;

public void setStatusBind(RichInputText statusBind) {
this.statusBind = statusBind;
}

public RichInputText getStatusBind() {
return statusBind;
}

public void setLinkBind(RichInputText linkBind) {
this.linkBind = linkBind;
}

public RichInputText getLinkBind() {
return linkBind;
}

 

Method to post a status on Facebook-

 

 /**Method to post facebook status on your timeline
  * @param actionEvent
  */
public void postFacebookStatusAction(ActionEvent actionEvent) {
//Get Access Token
String accessToken = fbTokenBind.getValue().toString();
//Get Id
String id = idBind.getValue().toString();

System.out.println(accessToken + " " + id);
//Initiate facebook client for accessToken
fbClient = new DefaultFacebookClient(accessToken);
//If status is with some url
if (statusBind.getValue() != null && linkBind.getValue() != null) {
fbClient.publish(id + "/feed", FacebookType.class, Parameter.with("message", statusBind.getValue()),
Parameter.with("link", linkBind.getValue()));
}
//If it is simple text status
else if (statusBind.getValue() != null) {
fbClient.publish(id + "/feed", FacebookType.class, Parameter.with("message", statusBind.getValue()));
}
}

Now run application and post status without url at first

 

Click on post button and check in the Facebook timeline, and you can see that status shows that it is posted using Graph API Explorer

Now check second case – Facebook status with a web URL

 

On the timeline, status is posted with weblink preview

Post Status on Facebook


Sample ADF Application (JDeveloper 12.1.3) – Download

Cheers 🙂 Happy Learning

Related Posts

6 thoughts on “Post Status on Facebook Timeline using restfb Graph API in ADF & Java”

  1. Your blog is really good I like this article and it is very well done thanks for giving such an awesome blog I fell overwhelmed by learning your blog.

Leave a Reply

Your email address will not be published. Required fields are marked *