【发布时间】:2013-06-24 23:47:01
【问题描述】:
我有一个 Android 应用程序,它显示来自特定用户的 Twitter 响应。自从升级到 API 1.1 版以来,我一直在尝试让 OAUTH2 应用程序仅进行身份验证,但是当我发送消费者密钥和秘密时,我收到了错误 400 响应。
代码如下 - 任何帮助将不胜感激。
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();
【问题讨论】: