前言
因为工作的原因,我头一次接触到了对接官方API,对接主要是针对B2C业务或者B2B、O2O这些,那话不多说上教程
你需要先去官方平台拿到appkey和appsecrt,苏宁开放平台:https://open.suning.com/
官方也有SDK文件,也可拿来参考,官方只有PHP和Java,下面是node.js版本
let appKey = your_appKey;
let appSecret = your_appSecret;
//对接的接口,这里示例是店铺API
let appMethod = "suning.custom.shopinfo.query";
//店铺API请求JSON格式
let params = "{
"sn_request": {
"sn_body": {
"queryShopinfo": {
"shopId": "your_shopId"
}
}
}
}"
//版本号
let versionNo = "v1.2";
let url, result, headers;
//获取当前时间 格式 Y-M-D h:m:s
var curTime = common.timeHelper.format();
加密秘钥 //请求报文base4加密
params_base64 = common.stringHelper.base64_encode(params);
//拼接起来去md5,32位大写,获取到加密秘钥
var signinfo = common.stringHelper.md5(appSecret + appMethod + curTime + appKey + versionNo + params_base64, "hex", false);
url = 'https://open.suning.com/api/http/sopRequest/' + appMethod;
请求头文件 headers = {
"Content-Type": "text/xml; charset=utf-8",
"AppMethod": appMethod,
"AppRequestTime": curTime,
"Format": "json",
"signInfo": signinfo,
"AppKey": appKey,
"VersionNo": versionNo,
"User-Agent": "suning-sdk-php",
"Sdk-Version": "suning-sdk-php-beta0.1"
};
// 拼接三个参数文件去请求
result = await common.httpHelper.json_normal(url, params, headers);
//转化为字符串输出
return common.stringHelper.json_encode(result);
};