我正在尝试使用网站的OAuth,该网站要求签名方法仅是“ HMAC-SHA1”。
我想知道如何在Python中实现此功能?
假单胞菌:
def sign_request(): from hashlib import sha1 import hmac # key = b"CONSUMER_SECRET&" #If you dont have a token yet key = b"CONSUMER_SECRET&TOKEN_SECRET" # The Base String as specified here: raw = b"BASE_STRING" # as specified by OAuth hashed = hmac.new(key, raw, sha1) # The signature return hashed.digest().encode("base64").rstrip('\n')
签名错误通常驻留在基本字符串中,请确保您理解这一点(如OAuth1.0规范在此处所述:http://tools.ietf.org/html/draft-hammer- oauth-10#section-3.4 。 1)。
以下输入用于生成签名基本字符串:
file=vacation.jpg &oauth_consumer_key=dpf43f3p2l4k3l03 &oauth_nonce=kllo9940pd9333jh &oauth_signature_method=HMAC-SHA1 &oauth_timestamp=1191242096 &oauth_token=nnch734d00sl2jdk &oauth_version=1.0 &size=original
串联和URL编码每个部分,最终结果为:
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