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;
}
}
To learn more please refer OAuth 2.0 tutoria l. Go to your Postman application and open the authorization tab. Select Oauth 2.0 authorization from the drop-down. Select Get New Access Token from the same panel.
Hit Submit and you will receive a Client Id and Client Secret. Note: Remember these are confidential values and should not be shared. To learn more please refer OAuth 2.0 tutoria l. Go to your Postman application and open the authorization tab. Select Oauth 2.0 authorization from the drop-down. Select Get New Access Token from the same panel.
Authenticating by encoding through Postman 1.Erase the key value pair that we entered earlier so that it now has no values. 2.Go to the authorization tab 3.Select Basic Auth in the Type dropdown 4.Enter username as postman and password as password 5.Press Preview Request 6.Go to Header and see that ...
I would recommend trying to complete that tutorial first. If you can create a REST API call successfully via calling java in the command line and using that access token, then you should be able to take that info on to the next steps in using Postman.
As per the code you have posted.. you are not using the signpost library for generating signature. You are using your custom code for it.
You can use signpost library as follow:
//create an oAuth consumer and provide CONSUMER_KEY & CONSUMER SECRET.
DefaultOAuthConsumer defaultOAuthConsumer = new DefaultOAuthConsumer("CONSUMER_KEY","CONSUMER_SECRET");
//REQUEST URL
String url = "https://oauth.withings.com/account/request_token";
try {
// sign the url with consumer. (This will add all oAuth parameters to the query automatically and return the signed request url with all parameter).
url = defaultOAuthConsumer.sign(url);
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
// use the url to make your request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With