0

As was described in this answer i've implement HMAC-SHA1 signature method.

def sign_request():
    from hashlib import sha1
    import hmac

    key = b"CONSUMER_SECRET&"  
    basestr = b"BASE_STRING"
    hashed = hmac.new(key, basestr, sha1)
    return hashed.digest().encode("base64").rstrip('\n')

But i've got AttributeError, 'bytes' object has no attribute 'encode'. As i understood, that is why i'm using Python3, but i don't know how to fix it.

1 Answer 1

3

That is because it is a byte and you are trying to encode like string. I fixed it:

from base64 import encodebytes
def sign_request():
    from hashlib import sha1
    import hmac

    key = b"CONSUMER_SECRET&"  
    basestr = b"BASE_STRING"
    hashed = hmac.new(key, basestr, sha1)
    return str(encodebytes(hashed.digest())).rstrip('\n')
print(sign_request())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.