I have an Android app that shows Twitter responses from one specific user. Since the upgrade to version 1.1 of the API I've been trying to get the OAUTH2 application only authentication working, but when I am sending the consumer key and secret, I am getting an error 400 response.
The code is below - any help would be appreciated.
HttpClient httpclient = new DefaultHttpClient();
uriString = "https://api.twitter.com/oauth2/token";
HttpPost httppost = new HttpPost(uriString);
HttpParams httpParams = httppost.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams, 15000);
String base64EncodedString =null;
try {
String encodedConsumerKey = URLEncoder.encode("twitter_consumer_key","UTF-8");
String encodedConsumerSecret = URLEncoder.encode("twitter_consumer_secret","UTF-8");
String authString = encodedConsumerKey +":"+encodedConsumerSecret;
base64EncodedString = Base64.encodeToString(authString.getBytes("UTF-8"), Base64.DEFAULT);
} catch (Exception ex) {
//do nothing for now...
}
httppost.setHeader(AUTHORIZATION, "Basic " + base64EncodedString);
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
HttpResponse response =null;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("grant_type", "client_credentials"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));
response = httpclient.execute(httppost);
statusCode = response.getStatusLine().getStatusCode();