Friday, April 15, 2011

Appengine twitter

While I am looking for answer how my app engine application can twitt automaitcally when it gets data, I found that there is no clear answers.

I do not really need to have oAuth since it is my twitter account and app engine server side needs to twitter.

Also, I didn't want to write to get consumer key or secret. all tutorials on web is started how to generate consumer key/secret from coding...

Eventually, by registering your app via https://dev.twitter.com/apps/new  will give all keys what I need. basically, you can get cinsuer_key. consumer_secret, access_token, access_token_secret from the panel once you register.very simple.


Then, now, as you can see the below code, it can make your app engine to tweet via your twitter acount whenever data is received fron your client app to app engine server. easy and fun!




import twitter4j.Twitter;

import twitter4j.TwitterException;

import twitter4j.TwitterFactory;

import twitter4j.auth.AccessToken;


public class TwitterManager {

private static final String betaCONSUMER_KEY = "Jd28exxxxxxxxxxxxxOw";

private static final String betaCONSUMER_SECRET = "2aCSkyWxxxxxxxxxxg";

private static final String betaACCESS_TOKEN = "2658xxxxxxxxxxxxxxxxBD";

private static final String betaACCESS_TOKEN_SECRET = "tExxxxxxxxxxnYGHbk";


public TwitterManager() {

// TODO Auto-generated constructor stub

}


public String updateStatus(String message, boolean isBeta) throws TwitterException{

    String s = "No";

    AccessToken accessToken;

    

    accessToken = new AccessToken(betaACCESS_TOKEN, betaACCESS_TOKEN_SECRET);

    

    

    Twitter twitter = new TwitterFactory().getInstance();

     twitter.setOAuthConsumer(betaCONSUMER_KEY, betaCONSUMER_SECRET);  

    twitter.setOAuthAccessToken(accessToken);

try {

twitter.updateStatus(message);

s = "Successfully updated the status.";

} catch (TwitterException e) {

s = e.getMessage();

}

       

        return s;

}

0 comments:

Post a Comment