TwitterOauth { public static String getOautHeaderString(Credentials credentials, String method, String baseUrl, Map<String, String> params) { String nonce = UUID.randomUUID().toString(); int time = Math.round(System.currentTimeMillis()/1000L); return getOautHeaderString(credentials, method, baseUrl, params, nonce, time); }  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 testOauthHeaderString() { String consumerKey = "xvz1evFS4wEEPTGEFPHBog"; String nonce = "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg"; String signature = "tnnArxj06cWHq44gCs1OSKk/jLY="; int time = 1318622958; String token = "370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb"; String expected = "OAuth oauth_consumer_key=\"xvz1evFS4wEEPTGEFPHBog\", oauth_nonce=\"kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg\", oauth_signature=\"tnnArxj06cWHq44gCs1OSKk%2FjLY%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1318622958\", oauth_token=\"370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb\", oauth_version=\"1.0\""; Assert.assertEquals(expected, TwitterOauth.getOautHeaderString(consumerKey, signature, token, nonce, time)); }
@Test public void testOauthHeaderStringComplete() { TwitterOauth.Credentials credentials = new TwitterOauth.Credentials(); credentials.setConsumerKey("xvz1evFS4wEEPTGEFPHBog"); credentials.setConsumerSecret("kAcSOqF21Fu85e7zjz7ZN2U4ZRhfV3WpwPAoE3Z7kBw"); credentials.setAccessToken("370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb"); credentials.setTokenSecret("LswwdoUaIvS8ltyTt5jkRh4J50vUPVVHtR2YPi5kE"); String method = "post"; String baseUrl = "https: String nonce = "kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg"; int time = 1318622958; Map<String, String> params = new HashMap<>(); params.put("status", "Hello Ladies + Gentlemen, a signed OAuth request!"); params.put("include_entities", "true"); String header = TwitterOauth.getOautHeaderString(credentials, method, baseUrl, params, nonce, time); String expected = "OAuth oauth_consumer_key=\"xvz1evFS4wEEPTGEFPHBog\", oauth_nonce=\"kYjzVBB8Y0ZFabxSWbWovY3uYSQ2pTgmZeNu2VS4cg\", oauth_signature=\"tnnArxj06cWHq44gCs1OSKk%2FjLY%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1318622958\", oauth_token=\"370773112-GmHxMAgYyLbNEtIKZeRNFsMKPR9EyMZeS9weJAEb\", oauth_version=\"1.0\""; }