First of all you have to create a Twitter app, hence you have to go to dev.twitter.com and log in with your twitter account
Next create a new application with the details related to your app.
Then click on setting and change the Application Type to Read, Write and Access direct messages
Then click on update this twitter application's settings
Create my Access tokens
First of all you need the following libraries
common-codec-1.4.jar http://mirrors.ibiblio.org/pub/mirrors/maven2/commons-codec/commons-codec/1.4/commons-codec-1.4.jar
jtwitter.jar http://www.winterwell.com/software/jtwitter/jtwitter.jar
sign-core-1.2.1.1.jar http://code.google.com/p/oauth-signpost/downloads/detail?name=signpost-core-1.2.1.1.jar
Next create a Web application with Netbeans
In the index.jsp set the following code.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form name="form1" action="NewServlet" method="Post">
User:<input type="textfield" name="user"/><br/>
Message:<input type="textfield" name="message"/><br/>
<input type="submit" value="Enviar"/>
</form>
</body>
</html>
Next create a Servlet. You may call it NewServlet
with the following code:
package com.ejemplo;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import winterwell.jtwitter.OAuthSignpostClient;
import winterwell.jtwitter.Twitter;
public class NewServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String JTWITTER_OAUTH_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxx";
String JTWITTER_OAUTH_SECRET = "XXXXXXXXXXXXXXXXXX";
String JTWITTER_ACCESS_TOKEN = "yyyyyyyyyyyyyyyyyyyyyyyyyy";
String JTWITTER_ACCESS_TOKEN_SECRET = "YYYYYYYYYYYYYYY";
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("Your Message was sent");
OAuthSignpostClient oauthClient = new OAuthSignpostClient(JTWITTER_OAUTH_KEY, JTWITTER_OAUTH_SECRET, JTWITTER_ACCESS_TOKEN, JTWITTER_ACCESS_TOKEN_SECRET);
Twitter twitter = new Twitter(request.getParameter("user"), oauthClient);
String fecha = (new Date()).toString();
twitter.updateStatus(request.getParameter("message") + fecha);
} catch (Exception ex) {
out.println(ex.getMessage());
ex.printStackTrace();
} finally {
out.close();
}
}
//
/**
* Handles the HTTP
GET
method.* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
POST
method.* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}//
}
Replace xxxxx with Consumer key
Replace XXXX with Consumer secret
Replace yyyyy with Access token
Replace YYYY with Access token secret
Finally upload you application to a webserver and run the app
No hay comentarios:
Publicar un comentario