References not available in the final program

Asked

Viewed 28 times

1

I’m validating some functions created in python, I have a code that creates some functions. When running the program with the functions (test_client.py), I get an error. auth_utils.py:

from hashlib import sha256
try:
    from urllib import urlencode
except ImportError:
    from urllib.parse import urlencode


def create_signature(secret_key, data=None):
    if data is None:
        data = {}
    url_encoded_data = urlencode(sorted(data.items()))
    hashed_data = sha256(secret_key + url_encoded_data)
    return hashed_data.hexdigest()

test_client.py:

from auth_utils import create_signature
from time import time


API_KEY = "kaakAYJ58EouuGW2"
API_SECRET = "HRN4Ig7BMB8xY9qVLJA7Ylzy"
DATA = {
    'first_name': 'Jonh',

error in execution:

$ python test_headers.py Traceback (Most recent call last): File "test_headers.py", line 13, in header = "Authorization Voxy {}:{}". format(API_KEY, create_signature(API_SECRET, DATA)) File "F: xampp htdocs proj Voxy auth_utils.py", line 12, in create_signature hashed_data = sha256(secret_key + url_encoded_data) Typeerror: Unicode-Objects must be encoded before hashing

  • I just didn’t understand the title of the question: what did you mean by "references are not available in the final program"?

1 answer

2


By the error, the object encoding is only missing when executing the hash. Try to apply the encoding, such as:

sha256(str(secret_key + url_encoded_data).encode('utf-8'))

Staying:

def create_signature(secret_key, data=None):
    if data is None:
        data = {}
    url_encoded_data = urlencode(sorted(data.items()))
    hashed_data = sha256(str(secret_key + url_encoded_data).encode('utf-8'))
    return hashed_data.hexdigest()

See working on Repl.it.

  • Thanks Anderson, thank you very much

  • I made the change, you are not running create_signature() in the test function.

  • With the same mistake?

  • Traceback (Most recent call last): File "test_headers.py", line 14, in <module> header = "Authorization Voxy {}:{}". format(API_KEY, create_signature(API_SECRET, DATA)) Nameerror: name 'create_signature' is not defined

  • there Repl.it worked

  • If you wish, you can edit your question by adding the file code test_headers.py. Apparently you’re not importing the function into the context correctly.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.