private String makeSignature ( String method , URL url , Map < String , String > authParams , Token token ) { //encode method and URL StringBuilder sb = new StringBuilder ( method + "&" + PercentEncoding . encode ( makeBaseUri ( url ) + url . getPath ( ) ) ) ; //encode parameters List < String > params = splitAndEncodeParams ( url ) ; for ( Map . Entry < String , String > p : authParams . entrySet ( ) ) { params . add ( PercentEncoding . encode ( p . getKey ( ) ) + "=" + PercentEncoding . encode ( p . getValue ( ) ) ) ; } //sort parameters and append them to the base string Collections . sort ( params ) ; StringBuilder pb = new StringBuilder ( ) ; for ( String p : params ) { if ( pb . length ( ) > 0 ) { pb . append ( "&" ) ; } pb . append ( p ) ; } sb . append ( "&" ) ; sb . append ( PercentEncoding . encode ( pb . toString ( ) ) ) ; //create base string and key for hash function String baseString = sb . toString ( ) ; String tokenSecret = token != null ? token . getSecret ( ) : "" ; String key = PercentEncoding . encode ( consumerSecret ) + "&" + PercentEncoding . encode ( tokenSecret ) ; //hash base string with the secret key try { SecretKeySpec secretKey = new SecretKeySpec ( key . getBytes ( UTF8 ) , HMAC_SHA1_ALGORITHM ) ; Mac mac = Mac . getInstance ( HMAC_SHA1_ALGORITHM ) ; mac . init ( secretKey ) ; byte [ ] bytes = mac . doFinal ( baseString . getBytes ( UTF8 ) ) ; return PercentEncoding . encode ( DatatypeConverter . printBase64Binary ( bytes ) ) ; } catch ( UnsupportedEncodingException e ) { //should never happen throw new RuntimeException ( e ) ; } catch ( NoSuchAlgorithmException e ) { //should never happen throw new RuntimeException ( e ) ; } catch ( InvalidKeyException e ) { //should never happen throw new RuntimeException ( e ) ; } }
Generates a OAuth signature from the HTTP method the URL and the authorization parameters