我正在尝试使用网站的OAuth,这要求签名方法仅为“HMAC-SHA1”.
我想知道如何在Python中实现它?
解决方法:
Pseudocodish:
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).
以下输入用于生成签名基本字符串:
> HTTP方法(例如GET)
>路径(例如http://photos.example.net/photos)
>参数,按字母顺序排列,例如(换行符为可读性):
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://photos.example.net/photos\u0026amp;文件= vacation.jpg&
oauth_consumer_key = dpf43f3p2l4k3l03&oauth_nonce = kllo9940pd9333jh&
oauth_signature_method = HMAC-SHA1&oauth_timestamp = 1191242096&
组oauth_token = nnch734d00sl2jdk&oauth_version = 1.0&大小=原