I am trying to invoke Nokias Get Body Measures api
https://api.health.nokia.com/measure?action=getmeas
Today, I have followed all the steps at https://developer.health.nokia.com/api but at the end of it i get the below mentioned 342 error
{"status":342,"error":"The signature (using Oauth) is invalid"}
--- EDIT ---- I need to build api for Get Body Measures API (https://developer.health.nokia.com/api/doc#api-Measure-get_measure)
Nokia apis uses OAuth 1.0. I have the consumer key, secret & user auth token & secret keys.
Below is the code i use to generate OAuth Signature but nokia apis complain that it is invalid.
I am not sure whats wrong in below mentioned code.. Please Help!
Thanks
package org.nokia.oauth.OAuthSignature;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
public class OAuthSignatureGenerator {
private static String key = "___";
private static String secret = "___";
private static final String HMAC_SHA1 = "HmacSHA1";
private static final String ENC = "UTF-8";
private static Base64 base64 = new Base64();
public static void main()
throws Exception {
String nonce = genNonce(32);
List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("action", "getmeas"));
qparams.add(new BasicNameValuePair("oauth_consumer_key", key));
qparams.add(new BasicNameValuePair("oauth_nonce", "" +nonce));
qparams.add(new BasicNameValuePair("oauth_signature_method", "HMAC-SHA1"));
qparams.add(new BasicNameValuePair("oauth_timestamp", "" + (System.currentTimeMillis() / 1000)));
qparams.add(new BasicNameValuePair("oauth_token","***********"));
qparams.add(new BasicNameValuePair("oauth_version", "1.0"));
qparams.add(new BasicNameValuePair("userid","************"));
String url = "http://api.health.nokia.com/measure";
StringBuilder base = new StringBuilder();
base.append("GET&");
base.append(URLEncoder.encode(url, ENC));
base.append("&");
base.append(URLEncoder.encode(URLEncodedUtils.format(qparams, ENC), ENC));
String sign = sign(base.toString());
List<NameValuePair> qparams1 = new ArrayList<NameValuePair>();
qparams1.add(new BasicNameValuePair("action", "getmeas"));
qparams1.add(new BasicNameValuePair("oauth_consumer_key", key));
qparams1.add(new BasicNameValuePair("oauth_nonce", "" + nonce));
qparams1.add(new BasicNameValuePair("oauth_signature",URLEncoder.encode(new1,ENC)));
qparams1.add(new BasicNameValuePair("oauth_signature_method", "HMAC-SHA1"));
qparams1.add(new BasicNameValuePair("oauth_timestamp", "" + (System.currentTimeMillis() / 1000)));
qparams1.add(new BasicNameValuePair("oauth_token","4995c0d05916b42f3e260554dd825bc3740997067ec223e7e81eb3b2dc36"));
qparams1.add(new BasicNameValuePair("oauth_version", "1.0"));
qparams1.add(new BasicNameValuePair("userid","10964064"));
ub.addParameters(qparams1);
URLEncodedUtils.format(qparams, ENC), null);
URI uri = ub.build();
System.out.println("URL with OAuth signature => "
+ uri.toString());
}
public static String sign(String signatureBaseString) {
try {
Mac mac = Mac.getInstance(HMAC_SHA1);
String oauth_token = "****";
byte[] keyBytes = (secret + "&" + oauth_token ).getBytes("UTF-8");
SecretKey key = new SecretKeySpec(keyBytes, HMAC_SHA1);
mac.init(key);
byte[] text = signatureBaseString.getBytes("UTF-8");
byte[] signatureBytes = mac.doFinal(text);
signatureBytes = Base64.encodeBase64(signatureBytes);
String signature = new String(signatureBytes, "UTF-8");
System.out.println("signature base: " + signatureBaseString);
System.out.println("signature: " + signature);
return signature;
}
catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
}
catch (InvalidKeyException e) {
throw new IllegalStateException(e);
}
catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static String genNonce(int length) {
String text = "";
String possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for(int i =0 ; i < length;i++) {
text += possible.charAt((int)Math.floor(Math.random() * possible.length()));
}
return text;
}
}