虎泰克博客

淘宝API签名异常,如何正确计算SIGN参数?(error code:25 Invalid Signature)


网上找到的资料太乱下面是最多的一种 1. 不需要在排好序的参数串后加APP_SECRET    

网上找到的资料太乱下面是最多的一种
1. 不需要在排好序的参数串后加APP_SECRET
     (注:注意看第二个原因。不加sign_method的情况下,有人说如果没有此参数,尾部不需要加secret,我没有验证。万一你一定要加sign_method,可以试试在尾部加和不加secret有什么区别)
2. API说sign_method是系统参数并且是必须的,事实上,可以不要。默认md5
3. 额外多出来一个partner_id参数,少了就说签名错误。
    partner_id=top-apitools
4. Session不是必须的,API的系统参数说明没有讲清楚,对于有些API,这个参数是必须的。在沙箱环境下,如果你不加Session,他会提示你出错的。
正确的调用方法:
使用沙箱测试,它会给出正确的拼凑URL,你根据它来准备参数,即可正常调用。
以 'taobao.item.get' 接口为例 我们实际需要参数如下: app_key     你应用的app_key fields           请求的返回字段(如detail_url,num_iid,title,nick...) method       接口名:taobao.item.get num_iid       商品的item id:例如20547892232 partner_id   莫名其妙蹦出来的参数,值为top-apitools
sign             计算出来的签名值。。最坑爹的一个参数 timestamp   签名之前无需encode,允许误差10分钟。yyyy-mm-dd HH:mm:ss,例如:2008-01-25 20:23:30 v                  version,固定值:2.0
这是第二种说法:
最近有朋友在用淘宝的API做东西,开发完成从沙箱迁移去正式环境,相同的代码得到错误提示Invalid Signature。 去open.taobao.com翻了一下文档,有一篇签名的生成规则,恩,淘宝制定了一套规范来生成signature,也就是http请求中sign参数的值。 规范本身很简单,只是很难书面描述清楚,给个例子,假设http请求是:http://gw.api.taobao.com/router/rest?app_key=1234567&product=name,price,id&method=product.search&sign_method=md5&v=2.0 需要做以下事情来生成signature: 1。把http的所有参数,也就是http://gw.api.taobao.com/router/rest?后面的东西,拷下来作为一个string,然后去掉所有的"&"和"=",得到字串:app_key1234567productname,price,idmethodproduct.searchsign_methodmd5v2.0 2。把淘宝给你的app secret加在字串的前、后,假设secret是“taobao_app_secret”,那么得到字串就是: taobao_app_secretapp_key1234567productname,price,idmethodproduct.searchsign_methodmd5v2.0taobao_app_secret 3。对刚才得到的字串做md5,得到摘要,就是signature,假设得到abcdef。然后需要在http请求后加一个&sign=abcdef 朋友试过N次,仍然得到Invalid signature,看了看程序,md5结果应该是对的。仔细对照淘宝api工具生成的请求,发现一个陷阱。淘宝服务器收到请求后,会做一个校验,也就是把刚才生成signature的过程做一边,如果得到的signature与你上传的sign参数的值不同,就会得到一个invalid signature错误。而这里的陷阱在于,淘宝服务器会先对http参数做一个排序,如果你本地的排序与淘宝不同,那么signature当然不会对。 利用淘宝的api测试工具可以很容易的看到它的排序: 例如某个api的顺序就是这样滴: app_key=1234567 format=xml method=taobao.shopcats.list.get sign_method=md5 timestamp=2010-06-22 11:04:15 v=2.0 sign=AD64DFFCBB86FF3839E6468BB490B404
照着它的顺序做了调整,done.


Statement: all articles / drawings reproduced on this website are for the purpose of transmitting more information and facilitating discussion, which does not mean that the website, the host and the organizer agree with their opinions or confirm the authenticity of their contents. The contents of the articles are for reference only. If the rights of the original author are violated, please contact the website administrator.

Sign in to comment

Comment list

    No data