Java tutorial
//package com.java2s; import android.net.Uri; import android.util.Base64; import android.util.Log; import java.util.ArrayList; import java.util.Collections; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; public class Main { public static String oauthConsumerKey; public static String oauthSecretKey; private static String generateSignature(String base, String type, String nonce, String timestamp, String token, String tokenSecret, String verifier, ArrayList<String> parameters) throws NoSuchAlgorithmException, InvalidKeyException { String encodedBase = Uri.encode(base); StringBuilder builder = new StringBuilder(); //Create an array of all the parameters //So that we can sort them //OAuth requires that we sort all parameters ArrayList<String> sortingArray = new ArrayList<String>(); sortingArray.add("oauth_consumer_key=" + oauthConsumerKey); sortingArray.add("oauth_nonce=" + nonce); sortingArray.add("oauth_signature_method=HMAC-SHA1"); sortingArray.add("oauth_timestamp=" + timestamp); sortingArray.add("oauth_version=1.0"); if (parameters != null) { sortingArray.addAll(parameters); } if (token != "" && token != null) { sortingArray.add("oauth_token=" + token); } if (verifier != "" && verifier != null) { sortingArray.add("oauth_verifier=" + verifier); } Collections.sort(sortingArray); //Append all parameters to the builder in the right order for (int i = 0; i < sortingArray.size(); i++) { if (i > 0) builder.append("&" + sortingArray.get(i)); else builder.append(sortingArray.get(i)); } String params = builder.toString(); //Percent encoded the whole url String encodedParams = Uri.encode(params); String completeUrl = type + "&" + encodedBase + "&" + encodedParams; String completeSecret = oauthSecretKey; if (tokenSecret != null && tokenSecret != "") { completeSecret = completeSecret + "&" + tokenSecret; } else { completeSecret = completeSecret + "&"; } Log.v("Complete URL: ", completeUrl); Log.v("Complete Key: ", completeSecret); Mac mac = Mac.getInstance("HmacSHA1"); SecretKeySpec secret = new SecretKeySpec(completeSecret.getBytes(), mac.getAlgorithm()); mac.init(secret); byte[] sig = mac.doFinal(completeUrl.getBytes()); String signature = Base64.encodeToString(sig, 0); signature = signature.replace("+", "%2b"); //Specifically encode all +s to %2b return signature; } }