php开发的API,采用TAuth2的sign签名认证,签名语句为:
urlencode(base64_encode(hex2bin(hash_hmac('sha1', $param, $tauth_token_secret))));
接口自动化框架是python的,因此需要在python中生成同样的签名以通过鉴权调用接口。
在网上查的下面的方法,怎么都不对,一层层的函数值对比跟踪,原来是php中多了一层hex2bin,所以需要增加对应的python方法binascii.a2b_hex()。
错误示范: urllib2.quote( base64.b64encode( hmac.new(self.tauth_token_secret,self.param,digestmod=hashlib.sha1).hexdigest()))) 正确示范: urllib2.quote( base64.b64encode( binascii.a2b_hex( hmac.new(self.tauth_token_secret,self.param,digestmod=hashlib.sha1).hexdigest())))
下面列一下php和python的函数对照关系: