TwitterOauth { public static String collectOauthParams(Map<String, String> params, String consumerKey, String nonce, int timestamp, String token) { Map<String, String> input = new HashMap<>(params); input.put("oauth_consumer_key", consumerKey); input.put("oauth_nonce", nonce); input.put("oauth_signature_method", "HMAC-SHA1"); input.put("oauth_timestamp", new Integer(timestamp).toString()); input.put("oauth_token", token); input.put("oauth_version", "1.0"); SortedSet<String> keys = new TreeSet<>(input.keySet()); StringBuilder sb = new StringBuilder(); boolean empty = true; for (String key : keys) { if (!empty) { sb.append("&"); } sb.append(percentEncode(key) + "=" + percentEncode(input.get(key))); empty = false; } return sb.toString(); }  static String getOautHeaderString(Credentials credentials, String method, String baseUrl, Map<String, String> params); static String getOautHeaderString(Credentials credentials, String method, String baseUrl, Map<String, String> params, String nonce, int time); static String getOautHeaderString(String consumerKey, String signature, String accessToken, String nonce, int time); static String percentEncode(String s); static String getSignatureBaseString(String method, String baseUrl, String collectedParams); static String getSigningKey(String consumerSecret, String tokenSecret); static String getHmacSha1(String data, String key); static String collectOauthParams(Map<String, String> params,
                                            String consumerKey,
                                            String nonce,
                                            int timestamp,
                                            String token);  }
@Test public void testCollectOauthParams() { Map<String, String> params = new HashMap<>(); params.put("status", "Hello Ladies + Gentlemen, a signed OAuth request!"); params.put("include_entities", "true"); String result = TwitterOauth.collectOauthParams(params, "xvz1evFS4wEEPTGEFPHBog", "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg", 1318622958, "370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb"); String expected = "include_entities=true&oauth_consumer_key=xvz1evFS4wEEPTGEFPHBog&oauth_nonce=kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1318622958&oauth_token=370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb&oauth_version=1.0&status=Hello%20Ladies%20%2B%20Gentlemen%2C%20a%20signed%20OAuth%20request%21"; Assert.assertEquals(expected, result); }