I have used the OAuth 1.0a and result is very successful when I get successful the access token, and then I am developing an application on Android use XAuth ,with experience in OAuth 1.0a I have done the XAuth but always get 401 UnAuthorize status code.I cannot understand I thik eevrything is correct.Please explain me.I am very grateful.Thank you very much.
Here is my code:
boolean flag=false;
try{
String oauth_nonce=getOAuthNonce();
String oauth_timestamp=getTimestamp();
String signatureBaseString =
“POST”
- “&”
- URLEncoder.encode(“https://api.twitter.com/oauth/access_token”)
- “&”
- URLEncoder.encode(“oauth_consumer_key=Cp3jukLlGarRUyk4DReoHg”)
- URLEncoder.encode("&" + “oauth_nonce=” + oauth_nonce)
- URLEncoder.encode("&" + “oauth_signature_method=” + “HMAC-SHA1”)
- URLEncoder.encode("&" + “oauth_timestamp=” + oauth_timestamp)
- URLEncoder.encode("&" + “oauth_version=” + “1.0”)
- URLEncoder.encode("&" + “x_auth_username=” + URLEncoder.encode(“XXXX”))
- URLEncoder.encode("&" + “x_auth_password=” + URLEncoder.encode(“XXXXX”))
- URLEncoder.encode("&" + “x_auth_mode=” + URLEncoder.encode(“client_auth”));
String oauth_signature=getSignatureToken(signatureBaseString, “HmacSHA1”,
“cB2kzOq64ZOO0dmqH3HhcRdMsC2xPr3a99FJOIC4j8”+"&");
String headerValue = “OAuth " +
“oauth_nonce=””+oauth_nonce+""," +
“oauth_signature_method=”"+“HMAC-SHA1”+""," +
“oauth_timestamp=”"+oauth_timestamp+""," +
“oauth_consumer_key=”"+“Cp3jukLlGarRUyk4DReoHg”+""," +
“oauth_signature=”"+URLEncoder.encode(oauth_signature,“UTF-8”)+""," +
“oauth_version=”"+“1.0”+""";
HttpPost httppost = new HttpPost(“https://api.twitter.com/oauth/access_token”
+"?x_auth_username="+URLEncoder.encode(“XXXX”)
+"&x_auth_password="+URLEncoder.encode(“XXXX”)
+"&x_auth_mode="+URLEncoder.encode(“client_auth”)
);
httppost.setHeader(“Host”,“api.twitter.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);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
jString=EntityUtils.toString(entity);
flag=true;
}
}else{
flag=false;
}
Log.e(“statusLine”, statusLine.getReasonPhrase());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
Log.e(“e”, e.getMessage());
} catch (IOException e1) {
// TODO Auto-generated catch block
Log.e(“e1”, e1.getMessage());
}
return flag;