JAVA integration
with twitter
Go
to the URL https://apps.twitter.com/
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.
And go to settings and check the permissions to read write and direct messages
Get
Consumer
Key
Consumer
Secret
see the screen
see the screen
And
also generate by clicking on generate access token it will display
Access
Token
Access
Token Secret.
Add
dependency
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>4.0.2</version>
</dependency>
Or
Else
we can add jar twitter4j-core
Code:
import
twitter4j.Twitter;
import
twitter4j.TwitterException;
import
twitter4j.TwitterFactory;
import
twitter4j.auth.AccessToken;
public
class
JavaTweet {
static
String consumerKeyStr
= "PASTE
CONSUMER KEY HERE";
static
String consumerSecretStr
= "PASTE
CONSUMER SECRET HERE";
static
String accessTokenStr
= "PASTE
ACCESS TOKEN HERE";
static
String accessTokenSecretStr
= "PASTE
ACCESS TOKEN SECRET HERE";
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("TWEET
ON TWITTER EXAMPLE");
System.out.println("Successfully
updated the status in Twitter.");
}
catch
(TwitterException te)
{
te.printStackTrace();
}
}
}
That’s
great. We have done it.
______________________________________________________________________________
We
have also a neither way to tweet using java by Twitter
REST API. For
that we need
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
All
these jar files.
And
code is as follows.
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
= "PASTE
CONSUMER KEY HERE";
static
String consumerSecretStr
= "PASTE
CONSUMER SECRET HERE";
static
String accessTokenStr
= "PASTE
ACCESS TOKEN HERE";
static
String accessTokenSecretStr
= "PASTE
ACCESS TOKEN SECRET HERE";
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%20Exammple.");
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()));
}
}
That’s
great. We have done it.
No comments:
Post a Comment