2

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();
4

1 回答 1

4

Looks like the problem was enconding the string to Base64 - I needed Base64.NO_WRAP rather than Base64.DEFAULT as this was throwing the string over two lines.

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.NO_WRAP); //Changed here!!!
} 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();
于 2013-06-24T15:47:01.790 回答