python实现php的签名,python和php中的hmac签名不同

在将Python脚本转换为PHP时遇到了API调用的HMAC签名不一致问题。已尝试多种方法,包括对比Python和PHP的body content MD5及string_to_sign输出,但仍然收到'签名不匹配'的错误。PHP代码中使用了hash_hmac函数生成授权头,涉及的内容包括URL、参数、时间戳和body内容。寻求帮助解决PHP HMAC签名生成的错误。
摘要由CSDN通过智能技术生成

我需要很少的帮助来解决php的问题hash_hmac。我正在将现有的python脚本转换为调用流程数据api的php。Python脚本运行良好。在

几乎没有类似的问题,其中之一是

HMAC value not consistent in Python and PHP但这对我没有帮助。我试过很多方法,但一直都有错误,你的签名不匹配

这是我的php代码$session_id = xxxxxxxxxxxxxxxxxxxxx;

$accessKey = xxxxxxxxxxxxxxxxxxxxxx;

$url = '/APIENDPOINT?action=mobileAccess&autoJoin=true';

$timestamp = date('r');

$body = '{"expireTime": "20160322T2359", "doorOperations": [{"operation": "guest", "doors": ["103"]}], "endPointID": "enpointID", "format": "rfid48"}';

$params["action"] = "mobileAccess";

$params["override"] = "true";

$canonicalized_query = array();

foreach ($params as $param => $value) {

$param = str_replace("%7E", "~", rawurlencode($param));

$value = str_replace("%7E", "~", rawurlencode($value));

$canonicalized_query[] = $param . "=" . $value;

}

ksort($canonicalized_query);

$canonicalized_query = implode("&", $canonicalized_query);

$string_to_sign = "POST\n";

$string_to_sign .= base64_encode(md5($body, true))."\n";

$string_to_sign .="application/json;charset=utf-8\n";

$string_to_sign .= $timestamp."\n";

$string_to_sign .= 'APIEDNPOINT?'.$canonicalized_query;

echo strlen($string_to_sign);

$header=array(

'Date: '.$timestamp,

'Content-Type: application/json;charset=utf-8',

'Content-MD5: '.base64_encode(md5($body, true)),

'Authorization: Basic '.$session_id.':'.base64_encode(hash_hmac('sha1', iconv(mb_detect_encoding($string_to_sign, mb_detect_order(), true), "UTF-8", $string_to_sign), iconv(mb_detect_encoding($accessKey, mb_detect_order(), true), "UTF-8", $accessKey), true))

这是工作python代码

^{pr2}$

在python中调用endpoint

^{3}$

我已经检查了两个脚本的body content md5 & string_to_sign输出是否相同。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值