API地址:
CA https://developer.walmart.com/ca/ca-mp/
US https://developer.walmart.com/home/us-mp/
一、生成密钥
CA站点
进入卖家后台,找到设置,点击生成。
US站点
与CA操作步骤一致。
二、使用
1.CA使用
请求头参数:
参数 | 取值 |
---|---|
WM_SVC.NAME | walmart |
WM_QOS.CORRELATION_ID | 随机生成的UUID,IdUtil.fastUUID() |
WM_SEC.TIMESTAMP | 当前时间戳,System.currentTimeMillis() |
WM_SEC.AUTH_SIGNATURE | 加密签名,下面说明如何生成 |
WM_CONSUMER.CHANNEL.TYPE | 后台授权生成的channel_type |
WM_CONSUMER.ID | 后台授权生成的consumer_id |
Accept | application/json,要求以json格式返回 |
加密签名:
public class WmSignUtils {
public WmSignUtils() {
}
public static String signData(String url, String consumerId, String privateKey, String requestMethod) {
String timestamp = String.valueOf(System.currentTimeMillis());
String signString = consumerId + "\n" + url + "\n" + requestMethod + "\n" + timestamp + "\n";
String signedString = signData(signString, privateKey);
return signedString;
}
private static String signData(String signString, String privateKey) {
String signatureString;
try {
byte[] encodedKeyBytes = Base64.decodeBase64(privateKey);
Signature signature = Signature.getInstance("SHA256withRSA");
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(encodedKeyBytes);
signature.initSign(KeyFactory.getInstance("RSA").generatePrivate(pkcs8EncodedKeySpec));
signature.update(signString.getBytes(StandardCharsets.UTF_8));
signatureString = Base64.encodeBase64String(signature.sign());
} catch (Exception e) {
throw new RuntimeException(e);
}
return signatureString;
}
privateKey由后台授权生成的私有key。
2.US使用
参数说明
参数名 | 取值 |
---|---|
WM_SVC.NAME | 可以固定Walmart |
WM_QOS.CORRELATION_ID | 随机生成的UUID,IdUtil.fastUUID() |
WM_SEC.ACCESS_TOKEN | token |
Accept | application/json,要求以json格式返回 |
如何获取token:
– Request URI: https://marketplace.walmartapis.com/v3/token
– Request method: POST
– Request headers:
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials");
Request request = new Request.Builder()
.url(getTokenUrl)
.method("POST", body)
.addHeader("WM_QOS.CORRELATION_ID", IdUtil.fastUUID())
.addHeader("WM_SVC.NAME", "walmart")
.addHeader("Authorization", Credentials.basic(clientId, clientSecret))
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("Accept","application/xml")
.build();
Response response = client.newCall(request).execute();
总结
Walmart US和CA授权完成,调用接口时,将请求头按格式处理即可调用。