Expand my Community achievements bar.

SOLVED

How to post data to twitter from website

Avatar

Former Community Member

Can anyone please help me how to post data to twitter account from website?

1 Accepted Solution

Avatar

Correct answer by
Administrator

Hi

As mentioned by Jitender, you can create a custom OSGI service/Component to do the needful.

You OSGi java code will depend on external dependency i.e. Twitter's Library.

You can refer to our community article that will help you create step by step a component that will use third party libraries.

Link:- https://helpx.adobe.com/experience-manager/using/aem_wordpress.html

 

Prerequisite – Twitter Keys

Before going to the code, we should generate Twitter API keys which enables us to access the APIs. These

We need to get “Consumer Key” and “Consumer Secret” from Twitter. Using these two, we should generate, “Access Token” and “Access Token Secret”.

Twitter Consumer Key and Consumer Secret

Go to the URL https://dev.twitter.com/apps and create a new application. Give access level in ‘Settings’ as ‘Read, Write and Access direct messages’. You will get both consumer key and secret.

Twitter Consumer Keys

 

Twitter Access Token

We can generate access tokens using the consumer keys. It can be done using the API or through the interface as well. I am going with generating using the Twitter website itself as shown below. Ensure that the access level comes the same as in consumer keys or regenerate it.

Twitter Access Token

Use Twitter REST API directly in Java

Now we are all set to post to Twitter using Java. In this section, let us see how we can use the REST APIs directly in our Java application. Let us use the status update post Twitter API 

All we need to do is,

set the auth credentials
invoke the URL https://api.twitter.com/1.1/statuses/update.json with required parameters
To make these REST call lets use oauth-signpost and Apache Commons HTTP. Following are the dependent jars to be used,

commons-codec-1.6.jar
commons-io-2.4.jar
commons-logging-1.1.3.jar
httpclient-4.3.1.jar
httpcore-4.3.jar
signpost-core-1.2.1.2.jar
signpost-commonshttp4-1.2.1.2.jar

//

package com.javapapers.java;import oauth.signpost.OAuthConsumer;import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;import org.apache.commons.io.IOUtils;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;public class JavaRestTweet {static String consumerKeyStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";static String consumerSecretStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";static String accessTokenStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";static String accessTokenSecretStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";public static void main(String[] args) throws Exception {OAuthConsumer oAuthConsumer = new CommonsHttpOAuthConsumer(consumerKeyStr,consumerSecretStr);oAuthConsumer.setTokenWithSecret(accessTokenStr, accessTokenSecretStr);HttpPost httpPost = new HttpPost("http://api.twitter.com/1.1/statuses/update.json?status=Hello%20Twitter%20World.");oAuthConsumer.sign(httpPost);HttpClient httpClient = new DefaultHttpClient();HttpResponse httpResponse = httpClient.execute(httpPost);int statusCode = httpResponse.getStatusLine().getStatusCode();System.out.println(statusCode + ':'+ httpResponse.getStatusLine().getReasonPhrase());System.out.println(IOUtils.toString(httpResponse.getEntity().getContent()));}}

 

Use Twitter API Wrapper – Twitter4J

Twitter4J is one of the most popular APIs for handling Twitter in Java. If we don’t want to handle the Twitter REST API calls directly as we have shown above and we want go simple then Twitter4J is the way. All we need to do is add only one jar file from Twitter4J twitter4j-core-3.0.5.jar to the dependency and use the respective classes to make the calls.

package com.javapapers.java;import twitter4j.Twitter;import twitter4j.TwitterException;import twitter4j.TwitterFactory;import twitter4j.auth.AccessToken;public class JavaTweet {static String consumerKeyStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";static String consumerSecretStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";static String accessTokenStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";static String accessTokenSecretStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";public static void main(String[] args) {try {Twitter twitter = new TwitterFactory().getInstance();twitter.setOAuthConsumer(consumerKeyStr, consumerSecretStr);AccessToken accessToken = new AccessToken(accessTokenStr,accessTokenSecretStr);twitter.setOAuthAccessToken(accessToken);twitter.updateStatus("Post using Twitter4J Again");System.out.println("Successfully updated the status in Twitter.");} catch (TwitterException te) {te.printStackTrace();}}}

Link:- http://javapapers.com/core-java/post-to-twitter-using-java/

I hope this would help you.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni

View solution in original post

2 Replies

Avatar

Level 9

sticky coder wrote...

Can anyone please help me how to post data to twitter account from website?

 

You could do so by using Java Twitter API. All you need to do is get their jar's in AEM and use API. Does not seem hard. 

For more details, here is a very good article

To get third party jars in AEM : http://www.cqblueprints.com/tipsandtricks/build-and-deploy-osgi/deploy-third-party-libs.html

http://javapapers.com/core-java/post-to-twitter-using-java/

Jitendra 

Avatar

Correct answer by
Administrator

Hi

As mentioned by Jitender, you can create a custom OSGI service/Component to do the needful.

You OSGi java code will depend on external dependency i.e. Twitter's Library.

You can refer to our community article that will help you create step by step a component that will use third party libraries.

Link:- https://helpx.adobe.com/experience-manager/using/aem_wordpress.html

 

Prerequisite – Twitter Keys

Before going to the code, we should generate Twitter API keys which enables us to access the APIs. These

We need to get “Consumer Key” and “Consumer Secret” from Twitter. Using these two, we should generate, “Access Token” and “Access Token Secret”.

Twitter Consumer Key and Consumer Secret

Go to the URL https://dev.twitter.com/apps and create a new application. Give access level in ‘Settings’ as ‘Read, Write and Access direct messages’. You will get both consumer key and secret.

Twitter Consumer Keys

 

Twitter Access Token

We can generate access tokens using the consumer keys. It can be done using the API or through the interface as well. I am going with generating using the Twitter website itself as shown below. Ensure that the access level comes the same as in consumer keys or regenerate it.

Twitter Access Token

Use Twitter REST API directly in Java

Now we are all set to post to Twitter using Java. In this section, let us see how we can use the REST APIs directly in our Java application. Let us use the status update post Twitter API 

All we need to do is,

set the auth credentials
invoke the URL https://api.twitter.com/1.1/statuses/update.json with required parameters
To make these REST call lets use oauth-signpost and Apache Commons HTTP. Following are the dependent jars to be used,

commons-codec-1.6.jar
commons-io-2.4.jar
commons-logging-1.1.3.jar
httpclient-4.3.1.jar
httpcore-4.3.jar
signpost-core-1.2.1.2.jar
signpost-commonshttp4-1.2.1.2.jar

//

package com.javapapers.java;import oauth.signpost.OAuthConsumer;import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;import org.apache.commons.io.IOUtils;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;public class JavaRestTweet {static String consumerKeyStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";static String consumerSecretStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";static String accessTokenStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";static String accessTokenSecretStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";public static void main(String[] args) throws Exception {OAuthConsumer oAuthConsumer = new CommonsHttpOAuthConsumer(consumerKeyStr,consumerSecretStr);oAuthConsumer.setTokenWithSecret(accessTokenStr, accessTokenSecretStr);HttpPost httpPost = new HttpPost("http://api.twitter.com/1.1/statuses/update.json?status=Hello%20Twitter%20World.");oAuthConsumer.sign(httpPost);HttpClient httpClient = new DefaultHttpClient();HttpResponse httpResponse = httpClient.execute(httpPost);int statusCode = httpResponse.getStatusLine().getStatusCode();System.out.println(statusCode + ':'+ httpResponse.getStatusLine().getReasonPhrase());System.out.println(IOUtils.toString(httpResponse.getEntity().getContent()));}}

 

Use Twitter API Wrapper – Twitter4J

Twitter4J is one of the most popular APIs for handling Twitter in Java. If we don’t want to handle the Twitter REST API calls directly as we have shown above and we want go simple then Twitter4J is the way. All we need to do is add only one jar file from Twitter4J twitter4j-core-3.0.5.jar to the dependency and use the respective classes to make the calls.

package com.javapapers.java;import twitter4j.Twitter;import twitter4j.TwitterException;import twitter4j.TwitterFactory;import twitter4j.auth.AccessToken;public class JavaTweet {static String consumerKeyStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";static String consumerSecretStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";static String accessTokenStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";static String accessTokenSecretStr = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";public static void main(String[] args) {try {Twitter twitter = new TwitterFactory().getInstance();twitter.setOAuthConsumer(consumerKeyStr, consumerSecretStr);AccessToken accessToken = new AccessToken(accessTokenStr,accessTokenSecretStr);twitter.setOAuthAccessToken(accessToken);twitter.updateStatus("Post using Twitter4J Again");System.out.println("Successfully updated the status in Twitter.");} catch (TwitterException te) {te.printStackTrace();}}}

Link:- http://javapapers.com/core-java/post-to-twitter-using-java/

I hope this would help you.

Thanks and Regards

Kautuk Sahni



Kautuk Sahni