【问题标题】:Oauth2 connection to twitter API from Android (Application Only Authentication)从 Android 到 twitter API 的 Oauth2 连接(仅应用程序身份验证)
【发布时间】: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();

【问题讨论】:

    标签: android twitter


    【解决方案1】:

    看起来问题在于将字符串编码为 Base64 - 我需要 Base64.NO_WRAP 而不是 Base64.DEFAULT,因为这会将字符串抛出两行。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-26
      • 2015-06-10
      • 2013-04-29
      • 1970-01-01
      • 2013-06-13
      • 2013-03-08
      • 2012-06-04
      • 1970-01-01
      相关资源
      最近更新 更多