Processing
 

Python HMAC hashed value encoding to base64

26/08/2016 09:43#1

Joined at: 5 months ago

Post: 3

Thank: 0

Thanked: 0

I am trying to make a twitter auth with the help of django middleware, where I calculate the signature of a request like this (https://dev.twitter.com/oauth/overview/creating-signatures):

    key = b"MY_KEY&"     raw_init = "POST" + "&" + quote("https://api.twitter.com/1.1/oauth/request_token", safe='')      raw_params = <some_params>     raw_params = quote(raw_params, safe='')      #byte encoding for HMAC, otherwise it returns "expected bytes or bytearray, but got 'str'"     raw_final = bytes(raw_init + "&" + raw_params, encoding='utf-8')      hashed = hmac.new(key, raw_final, sha1)      request.raw_final = hashed      # here are my problems: I need a base64 encoded string, but get the error "'bytes' object has no attribute 'encode'"     request.auth_header = hashed.digest().encode("base64").rstrip('\n') 

As you can see, there is no way to base64 encode a 'bytes' object.

The proposed solution was here: Implementaion HMAC-SHA1 in python

26/08/2016 11:20Top#2

Joined at: 5 months ago

Post: 1

Thank: 0

Thanked: 0

The trick is to use base64 module directly instead of str/byte encoding, which supports binary.

You can fit it like this (untested in your context, should work):

import base64 #byte encoding for HMAC, otherwise it returns "expected bytes or bytearray, but got 'str'" raw_final = bytes(raw_init + "&" + raw_params, encoding='utf-8')  hashed = hmac.new(key, raw_final, sha1)  request.raw_final = hashed  # here directly use base64 module, and since it returns bytes, just decode it request.auth_header = base64.b64encode(hashed.digest()).decode() 

For test purposes, find below a standalone, working example (python 3 compatible, Python 2.x users have to remove the "ascii" parameter when creating the bytes string.):

from hashlib import sha1 import hmac import base64  # key = CONSUMER_SECRET& #If you dont have a token yet key = bytes("CONSUMER_SECRET&TOKEN_SECRET","ascii")   # The Base String as specified here: raw = bytes("BASE_STRING","ascii") # as specified by oauth  hashed = hmac.new(key, raw, sha1)  print(base64.b64encode(hashed.digest()).decode()) 

result:

Rh3xUffks487KzXXTc3n7+Hna6o= 

PS: the answer your provided does not work anymore with Python 3. It's python 2 only.

Similar articles

How do twitter giveaways work?

9 months ago - Reply: 2 - Views: 130

How may I get genuine followers on Twitter?

3 years ago - Reply: 33 - Views: 1,832

Login to Twitter sometime fail

7 months ago - Reply: 1 - Views: 27

Twitter doesn't accept PHP's cURL, but Linux'

1 year ago - Reply: 2 - Views: 92

Twitter OAuth on iOS

6 months ago - Reply: 1 - Views: 96

why am i getting an error on .verifyCredentials()

6 months ago - Reply: 1 - Views: 37

TwitteR setup_twitter_oauth() gives an error

6 months ago - Reply: 1 - Views: 31

Tweepy - Getting the author of a retweeted tweet

6 months ago - Reply: 0 - Views: 7

Retrieving data from twitter with JavaScript

6 months ago - Reply: 0 - Views: 4

Combined social media feed returning the wrong data

6 months ago - Reply: 0 - Views: 3

Django and Twitter request_token

6 months ago - Reply: 0 - Views: 3

Python HMAC hashed value encoding to base64

6 months ago - Reply: 1 - Views: 35

At the rate symbol in facebook

6 months ago - Reply: 0 - Views: 3

vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re