public static String signRequest ( final URL url , final String yourGooglePrivateKeyString ) throws NoSuchAlgorithmException , InvalidKeyException , UnsupportedEncodingException , URISyntaxException { // Retrieve the proper URL components to sign final String resource = url . getPath ( ) + ' ' + url . getQuery ( ) ; // Get an HMAC-SHA1 signing key from the raw key bytes final SecretKeySpec sha1Key = new SecretKeySpec ( convertToKeyByteArray ( yourGooglePrivateKeyString ) , "HmacSHA1" ) ; // Get an HMAC-SHA1 Mac instance and initialize it with the HMAC-SHA1 // key final Mac mac = Mac . getInstance ( "HmacSHA1" ) ; mac . init ( sha1Key ) ; // compute the binary signature for the request final byte [ ] sigBytes = mac . doFinal ( resource . getBytes ( ) ) ; // base 64 encode the binary signature // Base64 is JDK 1.8 only - older versions may need to use Apache // Commons or similar. String signature = Base64 . getEncoder ( ) . encodeToString ( sigBytes ) ; // convert the signature to 'web safe' base 64 signature = signature . replace ( ' ' , ' ' ) ; signature = signature . replace ( ' ' , ' ' ) ; final String signedRequestPath = resource + "&signature=" + signature ; final String urlGoogleMapSignedRequest = url . getProtocol ( ) + "://" + url . getHost ( ) + signedRequestPath ; return urlGoogleMapSignedRequest ; }
Returns the full url as String object with the signature as parameter .