I am trying to use the OAuth of a website, which requires the signature method to be 'HMAC-SHA1' only.
I am wondering how to implement this in Python?
I am trying to use the OAuth of a website, which requires the signature method to be 'HMAC-SHA1' only.
I am wondering how to implement this in Python?
There are multiple python libraries available at the oauth website, but if you're just interested in a specific implementation you could have a look at one of them.
It's already there Keyed-Hashing for Message Authentication
Finally here's an actually working solution (tested with Python 3) utilizing oauthlib.
I use the first OAuth step given as an example in the official RTF 1:
The value for
oauth_signature
is what we would like to calculate.The following defines what we want to sign:
And here we go:
Pseudocodish:
Signature errors usually reside in the base-string, make sure you understand this (as stated by the OAuth1.0 spec here: http://tools.ietf.org/html/draft-hammer-oauth-10#section-3.4.1).
The following inputs are used to generate the Signature Base String:
Parameters, alphabetically, such as (line breaks for readability):
Concatenate and URL encode each part and it ends up as:
GET&http%3A%2F%2Fphotos.example.net%2Fphotos&file%3Dvacation.jpg%26 oauth_consumer_key%3Ddpf43f3p2l4k3l03%26oauth_nonce%3Dkllo9940pd9333jh%26 oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1191242096%26 oauth_token%3Dnnch734d00sl2jdk%26oauth_version%3D1.0%26size%3Doriginal
You can try following method.
In Python 3.7 there is an optimized way to do this. HMAC(key, msg, digest).digest() uses an optimized C or inline implementation, which is faster for messages that fit into memory.
https://docs.python.org/3/library/hmac.html#hmac.digest