Hi,
I have written a java program which wants to authenticate twitter account, I am passing &state=Customer_id to my url but while returning state is giving me null value.
here is my code
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
if (request.getParameter("oauth_verifier") != null) {
String customerId = request.getParameter("state") ;
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("MY_CONSUMER_KEY")
.setOAuthConsumerSecret(
"MY_CONSUMER_SECRET");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
String secretKey = "MY_CONSUMER_SECRET";
RequestToken requestToken = null;
try {
requestToken = twitter
.getOAuthRequestToken("http://127.0.0.1:8080/MyApp/twitterLogin");
} catch (TwitterException e1) {
System.out.println(e1);
}
try {
AccessToken accessToken = twitter.getOAuthAccessToken(
new RequestToken(request.getParameter("oauth_token"),
secretKey), request
.getParameter("oauth_verifier"));
System.out.println("accessToken ===" + accessToken.getToken());
System.out.println("accessToken Secret===" + accessToken.getTokenSecret());
ProfileConfigurationDAOImpl profileConfigurationDAOImpl = new ProfileConfigurationDAOImpl();
int count = profileConfigurationDAOImpl.updateAccessToken(accessToken.getToken(),customerId);
request.setAttribute("errorMessage",
"twitter authorization is done successfully");
response.sendRedirect("http://localhost:8080/MyApp/company/profile/myAccount");
} catch (TwitterException e) {
System.out.println("error == " + e);
} catch (SentimentAnalysisException e) {
e.printStackTrace();
}
} else {
String url = null;
ConfigurationBuilder cb = new ConfigurationBuilder();
// the following is set without accesstoken- desktop client
cb.setDebugEnabled(true)
.setOAuthConsumerKey("MY_CONSUMER_KEY")
.setOAuthConsumerSecret(
"MY_CONSUMER_SECRET");
try {
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
//request.getSession()
try {
UserBean userBean = new UserBean();
userBean = (UserBean) request.getSession(false).getAttribute(
"currentSessionUser");
RequestToken requestToken = twitter.getOAuthRequestToken("http://127.0.0.1:8080/MyApp/twitterLogin");
url = requestToken.getAuthorizationURL();
String url1 = url+"&state="+userBean.getCustomerId();
response.sendRedirect(url1);
} catch (IllegalStateException ie) {
// access token is already available, or consumer key/secret
// is
// not set.
if (!twitter.getAuthorization().isEnabled()) {
System.out
.println("OAuth consumer key/secret is not set.");
System.exit(-1);
}
}
} catch (TwitterException te) {
te.printStackTrace();
System.out
.println("Failed to get timeline: " + te.getMessage());
System.exit(-1);
}
}
}
Is this the right method to pass parameter in a url and get it back, as I want to maintain the information(Customer_id) as to who has authenticated the app.