Follow by Email

Saturday, 11 February 2017

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

Hello All

Previous post was about generating temporary access token and using it get Facebook profile detail usinf restfb Graph API , Go through 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 same sample application

There are two type of facebook status , first is simple text status and 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 status on faceook 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 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 pust status without url at first



Click on post button and check in 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 timeline status is posted with weblink preview



Sample ADF Application (Jdeveloper 12.1.3) - Download

Cheers :) Happy Learning

1 comment :