I am creating a web application that is trying to read twitter's profile, display his friends and show his photo. Sample code on Twitter4j website:
public static void main(String args[]) thrwos Exception{
Twitter twitter = new Twitter();
twitter.setOAuthConsumer("[consumer key]", "[consumer secret]");
RequestToken requestToken = twitter.getOAuthRequestToken();
AccessToken accessToken = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (null == accessToken) {
System.out.println("Open the following URL
and grant access to your account:");
System.out.println(requestToken.getAuthorizationURL());
System.out.print("Hit enter when it done.[Enter]:");
br.readLine();
try{
accessToken = requestToken.getAccessToken();
} catch (TwitterException te) {
if(401 == te.getStatusCode()){
System.out.println("Unable to get the access token.");
}else{
te.printStackTrace();
}
}
}
}
What am I doing in the servlet, give it the name IndexServlet I have the following code:
Twitter twitter = new Twitter();
twitter.setOAuthConsumer("[consumer key]", "[consumer secret]");
RequestToken requestToken = twitter.getOAuthRequestToken();
String authUrl = requestToken.getAuthorizationURL()
and then I pass authUrl to jsp so the user can click on it. It works so far, but after that I was already stuck! :)
I'm not sure where to set the callback url. Should I install it on the same servlet? The problem is that I must have the same twitter and requestToken variables as the first code with the main method, so I can do this:
accessToken = requestToken.getAccessToken();
Status status = twitter.updateStatus(some argument here);
URL- IndexServlet , Twitter. , , ,
if (already setup) {
accessToken = requestToken.getAccessToken();
Status status = twitter.updateStatus(some argument here);
}
, .
( , URL- )
twitter requestToken
, , twitter URL- , , -, .
, , RequestToken.
, , . , . !:)