2012-01-24 141 views
1

我正在嘗試使用Tumblr的Xuath。我已經通過電子郵件發送了Tumblr支持團隊來爲我的應用程序啓用Xuath,並且他們承擔了責任。但是,在嘗試檢索用戶密鑰和密碼時,我不斷收到'400:錯誤請求'錯誤。我無法找到調試不良請求的方法。Tumblr Xauth Android - 400錯誤請求錯誤

以下是代碼:(注意 - 此代碼已被來自全國各地的網絡可用段開發)

private final String CONSUMER_KEY = "mdMFLrprZGnRw4XO736GXcXP8huxaxTT5z1nlxDK38GbyWlW38"; 
private final String CONSUMER_SECRET = "VOpRNqKSLjhD3bR8vw4MorXgGc7lkT2FtBZr9xDchA5AvfscUI"; 

private final String ACCESS_URL = "https://www.tumblr.com/oauth/access_token"; 
private final String XAUTH_MODE = "client_auth"; 
private final String SIGNATURE_METHOD = "HMAC-SHA1"; 
private final String OAUTH_VERSION = "1.0"; 


private EditText mEmailAddress; 
private EditText mPassword; 
private Button mLogInButton; 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.tumblr_layout); 

    mEmailAddress = (EditText) findViewById(R.id.email_tumblr); 
    mPassword = (EditText) findViewById(R.id.passowrd_tumblr); 
    mLogInButton = (Button) findViewById(R.id.tumblr_login_button); 

    mLogInButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String email = mEmailAddress.getText().toString(); 
      String password= mPassword.getText().toString(); 



      String oauth_nonce = a64BitRandomString(); 
      String oauth_timestamp = getTimeStamp(); 


      String signatureBaseString = 
        "POST" 
        + "&" 
        + URLEncoder.encode(ACCESS_URL) 
        + "&" 
        + URLEncoder.encode("oauth_consumer_key=" + URLEncoder.encode(CONSUMER_KEY)) 
        + URLEncoder.encode("&" + "oauth_nonce=" + URLEncoder.encode(oauth_nonce)) 
        + URLEncoder.encode("&" + "oauth_signature_method=" + URLEncoder.encode(SIGNATURE_METHOD)) 
        + URLEncoder.encode("&" + "oauth_timestamp=" + URLEncoder.encode(oauth_timestamp)) 
        + URLEncoder.encode("&" + "oauth_version=" + URLEncoder.encode(OAUTH_VERSION)) 
        + URLEncoder.encode("&" + "x_auth_username=" + URLEncoder.encode(email)) 
        + URLEncoder.encode("&" + "x_auth_password=" + URLEncoder.encode(password)) 
        + URLEncoder.encode("&" + "x_auth_mode=" + URLEncoder.encode(XAUTH_MODE)); 


      String oauth_signature= getSignature(signatureBaseString, "HmacSHA1", 
        CONSUMER_SECRET+"&"); 

      try { 
       String headerValue = "OAuth " + 
         "oauth_nonce=\""+oauth_nonce+"\"," + 
         "oauth_signature_method=\""+SIGNATURE_METHOD+"\"," + 
         "oauth_timestamp=\""+oauth_timestamp+"\"," + 
         "oauth_consumer_key=\""+CONSUMER_KEY+"\"," + 
         "oauth_signature=\""+URLEncoder.encode(oauth_signature,"UTF-8")+"\"," + 
         "oauth_version=\""+OAUTH_VERSION+"\""; 

       HttpPost httppost = new HttpPost(ACCESS_URL 
         +"?x_auth_username="+URLEncoder.encode(email) 
         +"&x_auth_password="+URLEncoder.encode(password) 
         +"&x_auth_mode="+URLEncoder.encode(XAUTH_MODE));  

       httppost.setHeader("Host","https://www.tumblr.com"); 
       httppost.setHeader("Content-Type","application/x-www-form-urlencoded"); 
       httppost.setHeader("Authorization",headerValue); 


       // Execute HTTP Post Request 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpResponse response = httpclient.execute(httppost); // **I get the 401 error here** 
       StatusLine statusLine = response.getStatusLine(); 
       if (statusLine.getStatusCode() == HttpStatus.SC_OK) { 
       HttpEntity entity = response.getEntity(); 
       String jString= EntityUtils.toString(entity); 
       Log.d("TUMBLR - Value(s):", jString); 
       } 

      } catch (UnsupportedEncodingException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (ParseException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
    }); 

} 

private String a64BitRandomString() { 
    StringBuffer sb = new StringBuffer(); 
    Random generator = new Random(); 

    for (int i = 0; i < 32; i++) { 
     Integer r = generator.nextInt(); 
     if (r < 0) { 
      r = r * -1; 
     } 
     r = r % 16; 

     sb.append(Integer.toHexString(r)); 
    } 

    return sb.toString(); 
} 


private String getTimeStamp(){ 
    long seconds = (long) (System.currentTimeMillis()/1000.0); 
    String secondsString = String.valueOf(seconds); 
    return secondsString; 
} 

private String getSignature(String base, String mode, String secret) { 
    String signature = null; 


    SecretKeySpec key; 
    try { 
     key = new SecretKeySpec((secret).getBytes("UTF-8"), mode); 

     Mac mac = Mac.getInstance(mode); 
     mac.init(key); 

     byte[] bytes = mac.doFinal(base.getBytes("UTF-8")); 

     signature = new String(Base64.encode(bytes,Base64.NO_WRAP)); 
    } 
    catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (NoSuchAlgorithmException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (InvalidKeyException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return signature; 
} 

我明白,這將是非常困難的在這裏找出確切的錯誤。但是,如果有人能夠提供建議或指引我朝着正確的方向,我會很高興。

字符串headerValue具有以下值:

的OAuth oauth_nonce = 「8d0e6e03ae2424260ddd647d5afba70d」,oauth_signature_method = 「HMAC-SHA1」,oauth_timestamp = 「1327434351943」,oauth_consumer_key = 「mdMFLrprZGnRw4XO736GXcXP8huxaxTT5z1nlxDK38GbyWlW38」,oauth_signature =「cYNStrfA%2F2lTaGKL8pxWHpzSq9w %3D「,oauth_version =」1.0「

因此,它似乎是正確的格式。將字符串中的換行符包含在內會有幫助嗎?

回答

1

把我的頭髮撕裂了幾天後,事實證明我在HTTP post body中發送了太多的信息(因此,一個錯誤的請求錯誤正在被拋出)。所有這一切都需要的是下面的一組參數:

x_auth_username(用戶的電子郵件地址),x_auth_password和x_auth_mode = client_auth

而且,我應該利用的強勁的Oauth庫函數,而不是試圖用我有限的知識編寫自己的功能。在我的辯護中,無論如何,這些文檔都不夠清晰。非常好的一堂課 - 關於重新啓動代碼和使用常識。對於那些可能想知道是否有合法的TUMBLR Java客戶端的人來說 - 這裏是github鏈接:https://github.com/nsheridan/tumblr-java。我向你保證,這是我遇到的最棒的Tumblr代碼。

0

很多搜​​索的,在什麼地方找到下面的代碼嘰嘰喳喳後 -

  // replace with your username and password 
      String password = 「passwd」; 
      String userName = 「[email protected]」; 

      HttpPost httppost = new HttpPost("https://www.tumblr.com/oauth/access_token"); 
      CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(
        CONSUMER_KEY, CONSUMER_SECRET);  

      List<BasicNameValuePair> reqParams = Arrays.asList(
        new BasicNameValuePair("x_auth_username", userName), 
        new BasicNameValuePair("x_auth_password", password), 
        new BasicNameValuePair("x_auth_mode", "client_auth")); 

      AuthToken authToken = null; 
      try { 
       UrlEncodedFormEntity entity = new UrlEncodedFormEntity(reqParams, HTTP.UTF_8); 
       httppost.setEntity(entity); 
       consumer.sign(httppost); 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpResponse response = httpclient.execute(httppost); 

       StatusLine statusLine = response.getStatusLine(); 
       if (statusLine.getStatusCode() == HttpStatus.SC_OK) 
       { 
        InputStream data = response.getEntity() 
          .getContent(); 

        final char[] buffer = new char[0x10000]; 
        StringBuilder out = new StringBuilder(); 
        Reader in = new InputStreamReader(data, HTTP.UTF_8); 
        int read; 
        do { 
         read = in.read(buffer, 0, buffer.length); 
         if (read > 0) 
          out.append(buffer, 0, read); 
        } while (read >= 0); 
        in.close(); 
        String responseString = out.toString(); 

        String[] splitResponse = StringUtils.split(responseString, "&"); 
        String accessTokenSecret = getParameter(splitResponse, "oauth_token_secret"); 
        String accessToken = getParameter(splitResponse, "oauth_token"); 

       } 
      } catch (UnsupportedEncodingException e) { 
      } catch (OAuthMessageSignerException e) { 
      } catch (OAuthExpectationFailedException e) { 
      } catch (OAuthCommunicationException e) { 
      } catch (Exception e) { 
      }