Created
June 20, 2013 18:30
-
-
Save Capriatto/5825314 to your computer and use it in GitHub Desktop.
this class connect your twitter account with java to update your status' account.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import twitter4j.DirectMessage; | |
import twitter4j.Twitter; | |
import twitter4j.TwitterException; | |
import twitter4j.TwitterFactory; | |
import twitter4j.auth.OAuthAuthorization; | |
import twitter4j.conf.ConfigurationBuilder; | |
public class UpdateStatus { | |
private static final String ACCESS_TOKEN = "write your Access token"; | |
private static final String ACCESS_TOKEN_SECRET = "write your access token secret"; | |
private static final String CONSUMER_KEY = "write your consumer key"; | |
private static final String CONSUMER_SECRET = "write your consumer secret"; | |
public static void main(String[] args) { | |
ConfigurationBuilder builder = new ConfigurationBuilder(); | |
builder.setOAuthAccessToken(ACCESS_TOKEN); | |
builder.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET); | |
builder.setOAuthConsumerKey(CONSUMER_KEY); | |
builder.setOAuthConsumerSecret(CONSUMER_SECRET); | |
OAuthAuthorization auth = new OAuthAuthorization(builder.build()); | |
Twitter twitter = new TwitterFactory().getInstance(auth); | |
// The factory instance is re-useable and thread safe. | |
try { | |
/*Twitter sender = new TwitterFactory().getInstance(auth); | |
DirectMessage message = sender.sendDirectMessage(recipientId, message); | |
System.out.println("Sent: " message.getText() + " to @" + message.getRecipientScreenName());*/ | |
DirectMessage message = twitter.sendDirectMessage("@yourAccount", "Yep!, this is a message :)"); | |
System.out.println("Sender Screen Name " +message.getSenderScreenName()); | |
System.out.println("Sender Id " +message.getSenderId()); | |
System.out.println("Recipient Screen Name @"+message.getRecipientScreenName()); | |
System.out.println("Rccipient Id " +message.getRecipientId()); | |
//System.out.println("Sent: " message.getText()+ " to @ "+ message.getRecipientScreenName()); | |
twitter.updateStatus("First message via Java!"); | |
} catch (TwitterException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
System.out.println("Successfully updated the status."); | |
}// Don't forget to give permission "Read and write" when you create your application in dev.twitter.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment