Oauth 2.0, Problem in get Bearer Token


#1

Hi,

I am trying to implement the Twitter Search API V1.1

Please correct me if I am wrong.
I performed the below mentioned steps :

Step 1) Created an App in Twitter.
 So I got the TWITTER_CONSUMER_KEY and TWITTER_CONSUMER_SECRETCODE.

Step 2) I encoded the concatenation of the above keys separated by ":" with the base UTF-8.

Step3 ) Get the bearer token with the above generated code.

Step4 ) Use the bearer code to get the Tweets on the relevance of a keyword.

I am stuck in Step 3,

where in I am getting the Response as::

Server returned HTTP response code: 403 for URL: https://api.twitter.com/oauth2/token
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
	at com.tcs.crm.socialCRM.action.TwitterIntegration.requestBearerToken(TwitterIntegration.java:74)
	at com.tcs.crm.socialCRM.action.TwitterIntegration.getStatusSearch(TwitterIntegration.java:27)
	at com.tcs.crm.socialCRM.action.TwitterIntegration.main(TwitterIntegration.java:103)

My code is ::
// Method creating the Request
private String requestBearerToken(String endPointUrl, String encodedCredentials) throws IOException
{

		HttpsURLConnection connection = null;
		PrintWriter outWriter = null; 
		BufferedReader serverResponse = null;
		
		try 
		{
			URL url = new URL(endPointUrl); 
			connection = (HttpsURLConnection) url.openConnection();           
			connection.setDoOutput(true);
			connection.setDoInput(true); 
			connection.setRequestMethod("POST"); 
			connection.setRequestProperty("Host", "api.twitter.com");
			connection.setRequestProperty("User-Agent", "Search Tweets");
			connection.setRequestProperty("Authorization", "Basic " + encodedCredentials);
			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"); 
			connection.setRequestProperty("Content-Length", "29");
			connection.setUseCaches(false);
			connection.setDoOutput( true );

			
			//CREATE A WRITER FOR OUTPUT  
			outWriter = new PrintWriter( connection.getOutputStream() );  

			
			//SEND PARAMETERS  
			outWriter.println( "grant_type=client_credentials" );  
			outWriter.flush();  
			outWriter.close();  


			//RESPONSE STREAM  
			serverResponse = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );  

			JSONObject obj = (JSONObject)JSONValue.parse(serverResponse);
			
			
			logger.info("The return string is "+obj.toString());
			return  obj.toString();
	}
	catch (Exception e) 
	{
		e.printStackTrace();
		throw new IOException("Connection Issue");
	}
	finally 
	{
		if (connection != null) {
			connection.disconnect();
		}
	}
	
}

// Method Encoding the Keys :

public static String encodeKeysToBase64(String consumerKey, String consumerSecret)
{
try {
String encodedConsumerKey = URLEncoder.encode(consumerKey, “UTF-8”);
String encodedConsumerSecret = URLEncoder.encode(consumerSecret, “UTF-8”);

		        String fullKey = encodedConsumerKey + ":" + encodedConsumerSecret;
		        byte[] encodedBytes = Base64.encodeBase64(fullKey.getBytes());
		        return new String(encodedBytes); 
		    }
		    catch (UnsupportedEncodingException e) {
		        return new String();
		    }
	}

Please let me know where I am making the mistake.


#2

=>The First thing is You have to register your application here “https://dev.twitter.com/apps/new” by specifying required Information. Then click on TestOAuth you will be getting Consumer-Key and Consumer Secret key those you pass to your application. Along with these info you need to pass the same application name with in you java program.

=>Here to the requestBearerToken(String url) is “https://api.twitter.com/oauth2/token

=> To the fetchTimelineTweet(String url) is your EndPointURL :- “https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=<username>&count=50”.

Hope it will help you…
Thank you…