Google Map API Signature Generation in Python
Question: I'm basically doing the following:
<https://developers.google.com/maps/documentation/business/webservices/auth>

In Python 2.7.3 on my MacBook and on 2.7.5 on a Windows 64bit Server
environment I fail to reproduce the correct signature, while I follow the
original example exactly. I make a function like this:

    
    
    import sys
    import hashlib
    import urllib
    import hmac
    import base64
    import urlparse
    
    def process_url(input_url, private_key):
        print("URL To Sign: " + input_url)
        url = urlparse.urlparse(input_url)
        print("Private Key: " + private_key)
        url_to_sign = url.path + "?" + url.query
        print("Original Path + Query: " + url_to_sign)
        decoded_key = base64.urlsafe_b64decode(private_key)
        signature = hmac.new(decoded_key, url_to_sign, hashlib.sha1)
        encodedSignature = base64.urlsafe_b64encode(signature.digest())
        print("B64 Signature: " + encodedSignature)
        original_url = url.scheme + "://" + url.netloc + url.path + "?" + url.query
        full_url = original_url + "&signature=" + encodedSignature
        print "Full URL: " + full_url
        return full_url
    

Now this should give the following according to google:

  * URL: [http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=](http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=){clientID}
  * Private Key: vNIXE0xscrmjlyV-12Nj_BvUPaw=
  * URL Portion to Sign: /maps/api/geocode/json?address=New+York&sensor=false&client={clientID}
  * Signature: KrU1TzVQM7Ur0i8i7K3huiw3MsA=
  * Full Signed URL: [http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=](http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=){clientID}&signature=KrU1TzVQM7Ur0i8i7K3huiw3MsA=

However, when I do the following:

    
    
    process_url('http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client={clientID}', 'vNIXE0xscrmjlyV-12Nj_BvUPaw=')
    

I get:

  * URL To Sign: [http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=](http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=){clientID}
  * Private Key: vNIXE0xscrmjlyV-12Nj_BvUPaw=
  * Original Path + Query: /maps/api/geocode/json?address=New+York&sensor=false&client={clientID}
  * B64 Signature: WlcBIkr9WMB9uPhXWmAGcjG_2M4=
  * Full URL: [http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=](http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=){clientID}&signature=WlcBIkr9WMB9uPhXWmAGcjG_2M4=

So I get "WlcBIkr9WMB9uPhXWmAGcjG_2M4=" and not
"KrU1TzVQM7Ur0i8i7K3huiw3MsA=". I swear this used to work, but I get this new
W-value consistently over different systems.

Does anyone have any clue what I'm doing wrong? Is the page incorrect or am I
doing something basic incorrect??


Answer: You are not doing anything wrong, you are signing different data thus getting
a different signature.

In the Google example, if you copy and paste you'll notice that the braces are
omitted in the clipboard; so it's signing the following URL:

    
    
    http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID
    

But when you tried it, you copied it verbatim, including the curly braces:

    
    
    http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client={clientID}
    

So rejoice yourself, your code is right, the only problem is in the sample
data!

