1

I am working with Twitter api 1.1 Below is the code to get the bearer token from api 1.1. I need to set the content type of the request and send grant type=client_credentials in the body of the request. I am already sending the encoded consumer_key and consumer_secret in the header.

Can someone guide me how can I set the content and send parameter in the body of HttpPost request.

----------------------code starts here--------------------------------------------------------

 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.client.ResponseHandler; import org.apache.http.client.entity.UrlEncodedFormEntity; 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.Calendar; //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.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import quicktime.streaming.Stream; 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"; String authorization_header_string=consumer_key+":"+consumer_secret; authorization_header_string=URLEncoder.encode(authorization_header_string, "UTF-8"); System.out.println(authorization_header_string); HttpPost httppost = new HttpPost("https://api.twitter.com/oauth/token?grant_type=client_credentials"); httppost.setHeader("Authorization",authorization_header_string); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = httpclient.execute(httppost, responseHandler); System.out.println(responseBody); // oauth_token = responseBody.substring(responseBody.indexOf("oauth_token=") + 12, responseBody.indexOf("&oauth_token_secret=")); } } 

Thank you so much for your help.

1 Answer 1

1

Grant type should be set as a post parameter:

List<NameValuePair> parameters = new ArrayList<NameValuePair>(); parameters.add(new BasicNameValuePair("grant_type", "client_credentials")); post.setEntity(new UrlEncodedFormEntity(parameters)); 
Sign up to request clarification or add additional context in comments.

2 Comments

Twitter api is returning 500 Forbidden
I'm sorry but I don't know how the Twitter API functions with App Auth, all I know that the body of a request in case of a post are the post parameters

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.