Twitter API returning Forbidden error 500


#1

package com.memorial.twitter;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.client.HttpClient;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.BufferedReader;
//import java.io.IOException;
import java.io.InputStreamReader;
//import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
//import java.net.MalformedURLException;
//import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
//import java.util.Date;
import java.util.UUID;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import quicktime.streaming.Stream;

import com.meterware.httpunit.WebRequest;
import com.sun.tools.javac.util.Convert;

@WebServlet("/twitter_final")
public class Twitter_Final extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	 System.out.println("I am post");
	 String handle=request.getParameter("TwitterID");
     System.out.println(handle);                 
     HttpClient httpclient = new DefaultHttpClient();
     String consumer_key="consumer_key";
     String consumer_secret="consumer_secret";  
     consumer_key=URLEncoder.encode(consumer_key, "UTF-8");
     consumer_secret=URLEncoder.encode(consumer_secret,"UTF-8");
     String authorization_header_string=consumer_key+":"+consumer_secret;         
     System.out.println(authorization_header_string);   
     byte[] encoded = Base64.encodeBase64(authorization_header_string.getBytes());
     String encodedString = new String(encoded);
     System.out.println(encodedString);          
     HttpPost httppost = new HttpPost("https://api.twitter.com/oauth2/token");
     httppost.setHeader("Authorization",encodedString);
     List<NameValuePair> parameters = new ArrayList<NameValuePair>();
     parameters.add(new BasicNameValuePair("grant_type", "client_credentials"));
     httppost.setEntity(new UrlEncodedFormEntity(parameters));
     httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
     ResponseHandler<String> responseHandler = new BasicResponseHandler();
     String responseBody = httpclient.execute(httppost, responseHandler);
     System.out.println(responseBody);
 }

}

Response:-
HTTP Status 500 - Forbidden

type Exception report

message Forbidden

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.http.client.HttpResponseException: Forbidden
org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:67)
org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:54)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:734)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:708)
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:699)
com.memorial.twitter.Twitter_Final.doPost(Twitter_Final.java:99)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.37 logs.

Apache Tomcat/7.0.37


#2

The issue got resolved :slight_smile:

Following is the code that you can use to get the bearer token successfully from Twitter api 1.1

package com.memorial.twitter;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.client.HttpClient;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.BufferedReader;
//import java.io.IOException;
import java.io.InputStreamReader;
//import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
//import java.net.MalformedURLException;
//import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
//import java.util.Date;
import java.util.UUID;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import quicktime.streaming.Stream;

import com.meterware.httpunit.WebRequest;
import com.sun.tools.javac.util.Convert;

@WebServlet("/twitter_final")
public class Twitter_Final extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	 System.out.println("I am post");
	 String handle=request.getParameter("TwitterID");
     System.out.println(handle);                 
     HttpClient httpclient = new DefaultHttpClient();
     String consumer_key="consumer_key";
     String consumer_secret="consumer_secret";  
     consumer_key=URLEncoder.encode(consumer_key,"UTF-8");
     consumer_secret=URLEncoder.encode(consumer_secret,"UTF-8");
     String authorization_header_string=consumer_key+":"+consumer_secret;         
     System.out.println(authorization_header_string);   
     byte[] encoded = Base64.encodeBase64(authorization_header_string.getBytes());
     String encodedString = new String(encoded);
     System.out.println(encodedString); 
    // ---- Till here its fine
     HttpPost httppost = new HttpPost("https://api.twitter.com/oauth2/token");
     httppost.setHeader("Authorization","Basic "+encodedString);
     List<NameValuePair> parameters = new ArrayList<NameValuePair>();
     parameters.add(new BasicNameValuePair("grant_type","client_credentials"));
     httppost.setEntity(new UrlEncodedFormEntity(parameters));
     httppost.setHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
     ResponseHandler<String> responseHandler = new BasicResponseHandler();
     //------- The line below gives error
     String responseBody = httpclient.execute(httppost,responseHandler);
     System.out.println(responseBody);
     
 }

// protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// System.out.println(“I am Get”);
// }
}


#3

I got the answer, Following is the working code to retrive public tweets:

package com.memorial.twitter;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONSerializer;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.client.HttpClient;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.BufferedReader;
//import java.io.IOException;
import java.io.InputStreamReader;
//import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
//import java.net.MalformedURLException;
//import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
//import java.util.Date;
import java.util.UUID;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import quicktime.streaming.Stream;

import com.meterware.httpunit.WebRequest;
import com.sun.tools.javac.util.Convert;

@WebServlet("/twitter_final")
public class Twitter_Final extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	try{
	
	 String handle=request.getParameter("TwitterID");
     System.out.println(handle); // getting the username of any user as input in jsp file.....you can skip this part and give it just as an input too       
     HttpClient httpclient = new DefaultHttpClient();
     String consumer_key="consumer_key"; // consumer_key for your app
     String consumer_secret="consumer_secret";  //consumer_secret for your app
     consumer_key=URLEncoder.encode(consumer_key,"UTF-8");
     consumer_secret=URLEncoder.encode(consumer_secret,"UTF-8");
     String authorization_header_string=consumer_key+":"+consumer_secret;         
     System.out.println(authorization_header_string);   
     byte[] encoded = Base64.encodeBase64(authorization_header_string.getBytes());
     String encodedString = new String(encoded);
     System.out.println(encodedString); 
     HttpPost httppost = new HttpPost("https://api.twitter.com/oauth2/token");
     httppost.setHeader("Authorization","Basic "+encodedString);
     List<NameValuePair> parameters = new ArrayList<NameValuePair>();
     parameters.add(new BasicNameValuePair("grant_type","client_credentials"));
     httppost.setEntity(new UrlEncodedFormEntity(parameters));
     httppost.setHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
     ResponseHandler<String> responseHandler = new BasicResponseHandler();
     String responseBody = httpclient.execute(httppost,responseHandler);
     System.out.println(responseBody);
   
     //need to get the access_token from response and check whether the token is bearer token
     JSONObject obj;
obj = new JSONObject(responseBody);
     final String token_type =  (String) obj.get("token_type");
     System.out.println(token_type);
     final String bearer_token =  (String) obj.get("access_token");
     System.out.println(bearer_token);
   HttpGet httpget = new HttpGet("https://api.twitter.com/1.1/statuses/user_timeline.json?    count=20&screen_name="+handle); 
   String responseBody1="";
   httpget.setHeader("Authorization","Bearer "+bearer_token);
   HttpResponse response1 = httpclient.execute(httpget);
   responseBody1 = EntityUtils.toString(response1.getEntity());
  System.out.println(responseBody1); 
	}
     catch (JSONException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    
 }    

}


#4

I figured out the answer…in the authorization header I need to add the word basic
httppost.setHeader(“Authorization”,"Basic "+encodedString);


#5

And keep a space after Basic in the String