Post to twitter using Twitter4J API in Oracle ADF and Java

Sharing is Caring

Hello All
In this post, I am talking about how to post to twitter timeline using Twitter4J Java API in your Oracle ADF Application
Using this API makes the process very simple, For accessing twitter we need Twitter API keys, It is called Consumer Key and Access Token

For this, you need a twitter account and then follow these steps

Create a Twitter App

Go to Twitter Apps , Click on Create New App

Fill basic details as Application Name, Description and Website

Go to Permissions tab of app and allow access to Read, Write and Access direct messages

Now go to Keys and Access Tokens tab of app and note down Consumer key and Consumer Secret 

and there is a button to create Access Token  too (We will need it later)

Create Application to Tweet using Twitter4J API

Create a Fusion Web Application and attach twitter4j-core-4.0.4.jar to viewController project and create a page with one input text and a button

<af:panelFormLayout id="pfl1">
                    <af:inputText label="Your Text" id="it1" rows="3" contentStyle="width:350px;font-weight:bold;"
                                  labelStyle="font-weight:bold;font-size:medium;color:#167bb9;"
                                  binding="#{viewScope.TweetUsingJavaAPIBean.tweetTextBind}"/>
                    <af:link id="l1" icon="#{resource['images:tweet-button-2015.png']}"
                             actionListener="#{viewScope.TweetUsingJavaAPIBean.postTweetAction}"/>
                </af:panelFormLayout>

looks like this on page

Now see managed bean code that posts tweet

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import oracle.adf.view.rich.component.rich.input.RichInputText;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

import twitter4j.auth.AccessToken;

public class TweetUsingJavaAPIBean {

//Component Binding of input text
private RichInputText tweetTextBind;

public void setTweetTextBind(RichInputText tweetTextBind) {
this.tweetTextBind = tweetTextBind;
}

public RichInputText getTweetTextBind() {
return tweetTextBind;
}

public TweetUsingJavaAPIBean() {
}

// Put your app consumer key, secret and access token , secret here
private static final String consumerKey = “K************X”;
private static final String consumerSecret = “u******************************RrD”;
private static final String token = “4876****************************************7Vlg”;
private static final String tokenSecret = “H2u***********************************y9”;

/**Method to post tweet using Twitter4J API
* @param actionEvent
*/
public void postTweetAction(ActionEvent actionEvent) {
if (tweetTextBind.getValue() != null) {
try {
Twitter twitter = new TwitterFactory().getInstance();

twitter.setOAuthConsumer(consumerKey, consumerSecret);
AccessToken accessToken = new AccessToken(token, tokenSecret);

twitter.setOAuthAccessToken(accessToken);

twitter.updateStatus(tweetTextBind.getValue().toString());

} catch (TwitterException te) {
te.printStackTrace();
}
} else {
FacesMessage errMsg = new FacesMessage(“Tweet Can’t be empty”);
errMsg.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext.getCurrentInstance().addMessage(tweetTextBind.getClientId(), errMsg);
}
}
}

Now run and check application

 

On Twitter Timeline

Post to twitter

Sample ADF Application- Download

Cheers 🙂 Happy Learning

Related Posts

I’m an Oracle ACE, Blogger, Reviewer,
Technical Lead working on Oracle ADF

0 thoughts on “Post to twitter using Twitter4J API in Oracle ADF and Java”

Leave a Reply

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