Calculating a SHA hash with a string + secret key in python

前端 未结 5 613
我在风中等你
我在风中等你 2020-11-30 20:31

Amazon Product API now requires a signature with every request which I\'m trying to generate ushing Python.

The step I get hung up on is this one:

\"Calculat

5条回答
  •  無奈伤痛
    2020-11-30 21:02

    If you are trying to sign up a user to AWS cognito using Python3, you can use the following code.

    #For the SecretHash 
    import hmac
    import hashlib
    import base64   
    
    //Please note that the b in the secretKey and encode('utf-8') are really really important. 
    secretKey = b"secret key that you get from Coginito -> User Pool -> General Settings -> App Clients-->Click on Show more details -> App client secret  "
     clientId = "Coginito -> User Pool -> General Settings -> App Clients-->App client id"
     digest = hmac.new(secretKey,
                  msg=(user_name + clientId).encode('utf-8'),
                  digestmod=hashlib.sha256
                 ).digest()
     secrethash = base64.b64encode(digest).decode()
    

    The username user_name in the above is same as the user that you want to register in the cognito

    client = boto3.client('cognito-idp', region_name='eu-west-1' )

    response = client.sign_up(
                        ClientId='Coginito -> User Pool -> General Settings -> App Clients-->App client id',
                        Username='Username of the person you are planning to register',
                        Password='Password of the person you are planning to register',
                        SecretHash=secrethash,
                        UserAttributes=[
                            {
                                'Name': 'given_name',
                                'Value': given_name
                            },
                            {
                                'Name': 'family_name',
                                'Value': family_name
                            },
                            {
                                'Name': 'email',
                                'Value': user_email
                            }
                        ],
                        ValidationData=[
                            {
                                'Name': 'email',
                                'Value': user_email
                            },
                        ]
    

提交回复
热议问题