Skip to content

Instantly share code, notes, and snippets.

@socrateslee
Last active November 1, 2023 03:31
Show Gist options
  • Save socrateslee/76e91cb7beef6e0effa030b9e115333b to your computer and use it in GitHub Desktop.
Save socrateslee/76e91cb7beef6e0effa030b9e115333b to your computer and use it in GitHub Desktop.
京东联盟开放平台API的一个通用的Client封装
'''
京东联盟开放平台API的一个通用的Client封装。
京东联盟开放平台的文档详见 https://union.jd.com/openplatform
涉及到签名方法的文档见 https://union.jd.com/helpcenter/13246-13247-46301
Example:
client = JdApiClient("<YOUR_APP_KEY>", "<YOUR_SECRET_KEY>")
resp = client.call("jd.union.open.goods.promotiongoodsinfo.query",
{'skuIds':'12072066'})
print(resp.json())
'''
import datetime
import requests
import hashlib
import json
import urllib.parse
JD_API_ROOT = 'https://router.jd.com/api'
class JdApiClient(object):
def __init__(self, app_key, secret_key):
self.app_key = app_key
self.secret_key = secret_key
def get_sign(self, params):
params_list = sorted(list(params.items()), key=lambda x: x[0])
params_bytes = (self.secret_key + ''.join("%s%s" % (k, v) for k, v in params_list) + self.secret_key).encode('utf-8')
sign = hashlib.md5(params_bytes).hexdigest().upper()
return sign
def call(self, method, param_json, **kwargs):
params = {
"v": "1.0",
"method": method,
"app_key": self.app_key,
"timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"format": "json",
"sign_method": "md5"
}
if isinstance(param_json, (dict, list)):
params["param_json"] = json.dumps(param_json)
else:
params["param_json"] = param_json
params['sign'] = self.get_sign(params)
resp = requests.get(JD_API_ROOT, params=params, **kwargs)
return resp
@luzihang123
Copy link

luzihang123 commented Feb 22, 2020

京东的JdApiClient sign计算是在宙斯平台看的文档吗?

@martinadamsdev
Copy link

@luzihang123 直接京东联盟看的。但是我的 sign 打死不对。只能来网上照抄咯。

@luzihang123
Copy link

@luzihang123 直接京东联盟看的。但是我的 sign 打死不对。只能来网上照抄咯。

并没有在京东联盟看到sing计算方法,不过上述代码可以,哈哈。

@socrateslee
Copy link
Author

京东的JdApiClient sign计算是在宙斯平台看的文档吗?

这个文档见 https://union.jd.com/helpcenter/13246-13247-46301

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment