I am trying to hit the Oauth webservice which is of 1.0 Version. I can get this done succesfully over postman client but just can't get this done in android app.
Library used :- signpost-commonshttp4-1.2.1.2.jar
Parameters needed for the API :- oauth_consumer_key oauth_nonce oauth_version oauth_signature oauth_signature_method oauth_timestamp
Code :-
HttpClient httpclient = new DefaultHttpClient();
// generate the oauth_signature
String urlParamsForSignature = "oauth_consumer_key="+consumerKey +
"&oauth_nonce=" + "pT6c0H"+
"&oauth_signature_method=HMAC-SHA1" +
"&oauth_timestamp=" + timestamp +
"&oauth_version=1.0";
String baseString = "https://oauth.withings.com/account/request_token?" + urlParamsForSignature;
String signature = computeHmac(URLEncoder.encode(baseString), consumerSecret);
// add it to params list
qparams.add(new BasicNameValuePair("oauth_signature", signature));
// generate URI which lead to access_token and token_secret.
String urlParams = "oauth_consumer_key="+consumerKey +
"&oauth_nonce=" + "pT6c0H"+
"&oauth_signature=" + signature +
"&oauth_signature_method=HMAC-SHA1" +
"&oauth_timestamp=" + timestamp +
"&oauth_version=1.0";
String url = "https://oauth.withings.com/account/request_token?" + urlParams;
HttpGet httpget = new HttpGet(url);
// output the response content.
System.out.println("oken and Token Secrect:");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
int len;
byte[] tmp = new byte[2048];
while ((len = instream.read(tmp)) != -1) {
System.out.println(new String(tmp, 0, len, ENC));
}
}
public String computeHmac(String baseString, String key)
{
try {
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec secret = new SecretKeySpec(key.getBytes(), mac.getAlgorithm());
mac.init(secret);
byte[] digest = mac.doFinal(baseString.getBytes());
return new String(Base64.encodeBase64(digest));
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}