String requestBody = request.getBodyString();
String requestMethod = request.method;
if (consumerKey != null && consumerSecret != null) {
OAuthMessage message =
new OAuthMessage(requestMethod, requestUrl.toString(), null);
OAuthConsumer consumer =
new OAuthConsumer(null, consumerKey, consumerSecret, null);
consumer.setProperty(OAuth.OAUTH_SIGNATURE_METHOD, OAuth.HMAC_SHA1);
OAuthAccessor accessor = new OAuthAccessor(consumer);
if (accessToken != null) {
accessor.accessToken = accessToken;
accessor.tokenSecret = accessTokenSecret;
}
if (requestBody != null) {
if (signBodyHash.equals("true")) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] hash = md.digest(requestBody.getBytes("UTF-8"));
byte[] encodedHash = new Base64().encode(hash);
message.addParameter("oauth_body_hash",
new String(encodedHash, "UTF-8"));
} catch (java.security.NoSuchAlgorithmException e) {
// Ignore exception
} catch (java.io.UnsupportedEncodingException e) {
// Ignore exception
}
} else {
message.addParameter(requestBody, "");
}
}
try {
message.addRequiredParameters(accessor);
} catch (OAuthException e) {
throw new OpenSocialRequestException(
"OAuth error thrown while signing request " + e.getMessage());
} catch (java.net.URISyntaxException e) {
throw new OpenSocialRequestException(
"Malformed request URL " + message.URL + " could not be signed");
}
if (debug != null) {
try {
System.out.println("Signature base string:\n" +
net.oauth.signature.OAuthSignatureMethod.getBaseString(message));
} catch (URISyntaxException e) {
// Ignore exception
}
}
for (Map.Entry<String, String> p : message.getParameters()) {
if (!p.getKey().equals(requestBody)) {
requestUrl.addQueryStringParameter(p.getKey(), p.getValue());
}
}
}