I am trying to implement OAuth request to webservice using HmacSHA1 signature. I need to send two fields: data and method. Here is my code:
String HMAC_SHA1_ALGORITHM = "HmacSHA1";
String response = "";
try {
HttpClient client = new DefaultHttpClient();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
HttpPost request = new HttpPost(url);
CommonsHttpOAuthConsumer co = new CommonsHttpOAuthConsumer(key,
secret);
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(),
HMAC_SHA1_ALGORITHM);
// get an hmac_sha1 Mac instance and initialize with the signing
// key
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
// compute the hmac on input data bytes
byte[] rawHmac = mac.doFinal(data.getBytes());
// base64-encode the hmac
String result = new String(rawHmac, "UTF-8");// Encoding.EncodeBase64(rawHmac);
nameValuePairs.add(new BasicNameValuePair("data", result));
nameValuePairs.add(new BasicNameValuePair("method",
"UpdateFilms"));
co.sign(request);
request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = client.execute(request);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
response = sb.toString();
try {
} catch (Exception e) {
e.printStackTrace();
}
Log.v("response", "" + response);
} catch (Exception e) {
e.printStackTrace();
}
return response;
But the server is returning error "Invalid Signature". Is my implementation correct?
0 comments:
Post a Comment