package com.moms.test.twitter.library;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;
import twitter4j.internal.logging.Logger;
import net.htmlparser.jericho.*;
public class TwitterCom4j {
final String CONSUMER_KEY = "ZSPxNj5A5ZLkXpZtMUkZ7A";
final String CONSUMER_SECRET = "lITIhyMSZkcUsVoXt9l0kNbKLda3xURUlae1DgA1k";
public final static int NOT_USED = 0;
public final static int LOGGED_IN = -1;
public final static int TIME_OUT = -2;
public String username;
public String password;
public String accessToken;
public String accessTokenSecret;
public int currentState=0;
Twitter twitter = new TwitterFactory().getInstance();
public int Login(AuthToken authToken){
AccessToken token = new AccessToken(authToken.accessToken, authToken.accessTokenSecret);
this.twitter = new TwitterFactory().getOAuthAuthorizedInstance(CONSUMER_KEY, CONSUMER_SECRET, token);
currentState=LOGGED_IN;
return 1;
}
public AuthToken firstLogin(String userid, String password) {
AuthToken token = new AuthToken();
this.twitter = new TwitterFactory().getInstance();
this.username = userid;
this.password = password;
this.twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
RequestToken requestToken = null;
try {
requestToken = this.twitter.getOAuthRequestToken();
} catch (TwitterException e1) {
e1.printStackTrace();
}
String requestTokenUrl = requestToken.getAuthorizationURL();
/* String requestTokenString = requestToken.getToken();
String requestTokenSecretString = requestToken.getTokenSecret();
p("request token: "+ requestTokenString+"\n");
p("request token secret: "+ requestTokenSecretString+"\n");
p(requestTokenUrl);
*/
// PIN
String pin = getAccessPin(requestTokenUrl);
AccessToken accessToken = null;
try{
accessToken = twitter.getOAuthAccessToken(requestToken, pin);
} catch (TwitterException te) {
if(401 == te.getStatusCode()){
System.out.println("Unable to get the access token.");
token.status = -1;
return token;
}
else{
te.printStackTrace();
}
}
token.status = 1;
token.accessToken = accessToken.getToken();
token.accessTokenSecret = accessToken.getTokenSecret();
p(token.accessToken);
p(token.accessTokenSecret);
this.currentState = LOGGED_IN;
return token;
}
public int writeTweet(String content){
if(this.currentState != LOGGED_IN)
{
// token load
return -1;
}
if(content.length()> 140)
{
return -100;
}
// AccessToken token = new AccessToken(accessToken.getToken(), accessToken.getTokenSecret());
Status status = null;
try {
status = twitter.updateStatus(content);
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// p("updated the status to : " + status.getText());
return 0;
}
public String getAccessPin(String url){
String pin="";
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod(url);
int statusCode = 0;
try {
statusCode = client.executeMethod(getMethod);
} catch (HttpException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + getMethod.getStatusLine());
}
// Read the response body.
InputStream responseBody = null;
try {
responseBody = getMethod.getResponseBodyAsStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// p(responseBody);
Source source = null;
try {
source = new Source(responseBody);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
FormFields formFields=source.getFormFields();
// System.out.println("The document "+responseBody+" contains "+formFields.size()+" form fields:\n");
String authenticity_token = formFields.getValues("authenticity_token").get(0);
String oauth_token = formFields.getValues("oauth_token").get(0);
/*
p(authenticity_token);
p(oauth_token);
for (FormField formField : formFields) {
System.out.println(formField.getDebugInfo());
}
*/
//post id password
PostMethod postMethod = new PostMethod("http://twitter.com/oauth/authorize");
//postMethod.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
NameValuePair[] data = {
new NameValuePair("authenticity_token", authenticity_token),
new NameValuePair("oauth_token", oauth_token),
new NameValuePair("session[username_or_email]", this.username),
new NameValuePair("session[password]", this.password)
};
postMethod.setRequestBody(data);
//postMethod.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
// execute method and handle any error responses.
try {
statusCode = client.executeMethod(postMethod);
} catch (HttpException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Read the response body.
InputStream responseBodyForPin = null;
try {
responseBodyForPin = postMethod.getResponseBodyAsStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Source sourcePin = null;
try {
sourcePin = new Source(responseBodyForPin);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Element s = sourcePin.getAllElements("div id=\"oauth_pin\"").get(0);
if(s==null)
p("error!!");
//remove white space & CR
pin = s.getContent().toString().trim();
// p("pin:"+pin);
return pin;
}
public int getTimeline()
{
List<Status> statuses = null;
try {
statuses = twitter.getFriendsTimeline();
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Showing friends timeline.");
for (Status status : statuses) {
System.out.println(status.getUser().getName() + ":" +
status.getText());
}
return 1;
}
private void storeAccessToken(int useId, AccessToken accessToken){
this.accessToken = accessToken.getToken();
this.accessTokenSecret = accessToken.getTokenSecret();
/*
p("id = " + useId);
p("access token = " + accessToken);
p("token = " + accessToken.getToken());
p("secret = " + accessToken.getTokenSecret());
*/
}
private static void p(String s) {
System.out.println(s);
}
}
|