OAuth Request token in Twitter API JAVA JSP Example

Posted By: Matpal - June 26, 2011
Here is the code snippet which will tell you how to get request token in twitter OAuth API by JAVA. We create a JSP file say oauth.jsp . All you need to import few jars like -
  • Apache HttpClient
  • OAuth signepost library
And here is the JSP file



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


<%@ 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"%>

<%
OAuthConsumer consumer = new CommonsHttpOAuthConsumer
 ("<consumer key>","<consumer secret>");

OAuthProvider provider = new DefaultOAuthProvider(
                 "http://twitter.com/oauth/request_token",
                 "http://twitter.com/oauth/access_token", 
                 "http://twitter.com/oauth/authorize");



// Set your callback URL here
String authUrl = provider.retrieveRequestToken
           (consumer,"http://localhost:8080/twitter/callback.jsp");

session.setAttribute("consumer",consumer);
session.setAttribute("provider",provider);

out.println("<a href='"+authUrl+"'>Connect</a>");

%>

0 comments:

Post a Comment

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