vendredi 24 avril 2015

Correct way to call ALexa API

I am trying to implement Alexa api TrafficHistory in python . But I am getting this response

<?xml version="1.0"?>
<Response><Errors><Error><Code>InvalidParameterValue</Code><Message>Value (HmacSha256) for parameter SignatureMethod is invalid. Invalid signature method. Please consult the docs for the valid signature methods.</Message></Error></Errors><RequestID>ID</RequestID></Response>

Here is my code snippet :

def amazon_test_url():
    import base64, hashlib, hmac, time
    from urllib import urlencode, quote_plus

    AWS_ACCESS_KEY_ID = ''
    AWS_SECRET_ACCESS_KEY = ''  

    base_url = "http://ift.tt/1iGYhs6"
    url_params = dict(
        Action='TrafficHistory', 
        ResponseGroup='History',
        Url="http://google.com",
        AWSAccessKeyId=AWS_ACCESS_KEY_ID,
        SignatureVersion=2,
        SignatureMethod='HmacSha256')


    # Add a ISO 8601 compliant timestamp (in GMT)
    url_params['Timestamp'] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())

    # Sort the URL parameters by key
    keys = url_params.keys()
    keys.sort()
    # Get the values in the same order of the sorted keys
    values = map(url_params.get, keys)

    # Reconstruct the URL parameters and encode them
    url_string = urlencode(zip(keys,values))

    #Construct the string to sign
    string_to_sign =  url_string

    # Sign the request
    signature = hmac.new(
        key=AWS_SECRET_ACCESS_KEY,
        msg=string_to_sign,
        digestmod=hashlib.sha256).digest()

    # Base64 encode the signature
    signature = base64.encodestring(signature).strip()

    # Make the signature URL safe
    urlencoded_signature = quote_plus(signature)
    url_string += "&Signature=%s" % urlencoded_signature

    print "%s?%s\n\n%s\n\n%s" % (base_url, url_string, urlencoded_signature, signature)

    url = base_url + "?" + url_string + urlencoded_signature + signature

    r = requests.get(url)
    print r.text

What is the correct way ?




Aucun commentaire:

Enregistrer un commentaire