Post tweet by OAuth JAVA JSP Example

Posted By: Matpal - June 26, 2011
Posting tweet in twitter is very simple with JSP. Its a two step process-

Step 1 : Get access token and secret Click Here

Step 2: Post tweet here


<%@ page import="java.io.BufferedReader"%>
<%@ page import="java.io.InputStreamReader"%>
<%@ page import="java.net.URLEncoder"%>

<%@ page import="oauth.signpost.OAuth"%>
<%@ page import="oauth.signpost.OAuthConsumer"%>
<%@ page import="oauth.signpost.OAuthProvider"%>
<%@ page import="oauth.signpost.basic.DefaultOAuthProvider"%>
<%@ page import="oauth.signpost.commonshttp.CommonsHttpOAuthConsumer"%>


<%@ page import="org.apache.http.HttpResponse"%>
<%@ page import="org.apache.http.client.HttpClient"%>
<%@ page import="org.apache.http.client.methods.HttpPost"%>
<%@ page import="org.apache.http.entity.StringEntity"%>
<%@ page import="org.apache.http.impl.client.DefaultHttpClient"%>
<%@ page import="org.apache.http.client.methods.HttpEntityEnclosingRequestBase"%>


<%
OAuthConsumer consumer = (OAuthConsumer)session.getAttribute("consumer");
OAuthProvider provider = (OAuthProvider)session.getAttribute("provider");

String token=(String)session.getAttribute("token");
String secret=(String)session.getAttribute("secret");



consumer.setTokenWithSecret(token,secret);


HttpPost requestU = new HttpPost(
        "http://api.twitter.com/1/statuses/update.xml");
StringEntity body = new StringEntity("status=Desicoding");
body.setContentType("application/x-www-form-urlencoded");
requestU.setEntity(body);

consumer.sign(requestU);



HttpClient httpClient = new DefaultHttpClient();
HttpResponse respo = httpClient.execute(requestU);

out.println("Response: "
        + respo.getStatusLine().getStatusCode() + " "
        + respo.getStatusLine().getReasonPhrase());

%>

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.