diff --git a/warehouse-sdk-edayun/src/main/java/common/ecommerce/edayun/EdayunConstants.java b/warehouse-sdk-edayun/src/main/java/common/ecommerce/edayun/EdayunConstants.java index 1b21b07..c5a0d58 100644 --- a/warehouse-sdk-edayun/src/main/java/common/ecommerce/edayun/EdayunConstants.java +++ b/warehouse-sdk-edayun/src/main/java/common/ecommerce/edayun/EdayunConstants.java @@ -20,12 +20,13 @@ public final class EdayunConstants { * *

TODO 待核对:等易达云提供测试域名后替换。

*/ - public static final String PR_BASE_URL = "https://TODO.edayun.example"; + public static final String PR_BASE_URL = "https://test.omniselling.com.cn/saas-service/openapi/microService"; /** * 生产环境基址。 * - *

TODO 待核对。

+ *

TODO 待核对:测试环境是 {@code test.omniselling.com.cn}, + * 生产域名应以正式文档为准。

*/ public static final String PRODUCTION_BASE_URL = "TODO"; @@ -35,7 +36,12 @@ public final class EdayunConstants { /** Content-Type。 */ public static final String CONTENT_TYPE_JSON = "application/json"; - /** 请求头:token(来自 {@code POST /oauth/token})。 */ + /** + * 请求头:token(来自 {@code POST /oauth/token})。 + * + *

仅业务接口使用。{@code POST /oauth/token} 本身不带这个头—— + * 文档里列出它是全局头定义,不是该接口的参数。

+ */ public static final String HEADER_TOKEN = "token"; /** 请求头:appKey。 */ diff --git a/warehouse-sdk-edayun/src/main/java/common/ecommerce/edayun/http/EdayunRequestFactory.java b/warehouse-sdk-edayun/src/main/java/common/ecommerce/edayun/http/EdayunRequestFactory.java index d6b46c2..2c846ad 100644 --- a/warehouse-sdk-edayun/src/main/java/common/ecommerce/edayun/http/EdayunRequestFactory.java +++ b/warehouse-sdk-edayun/src/main/java/common/ecommerce/edayun/http/EdayunRequestFactory.java @@ -75,12 +75,19 @@ public final class EdayunRequestFactory { public HttpRequest business(String baseUrl, String path, String method, String token, String appKey, String appSecret, long timestamp, String bodyJson, String operation) { + return buildBusiness(baseUrl, path, method, token, appKey, + sign(bodyJson, appSecret), timestamp, bodyJson, operation); + } + + private HttpRequest buildBusiness(String baseUrl, String path, String method, + String token, String appKey, String signValue, + long timestamp, String bodyJson, String operation) { Map headers = new LinkedHashMap(); headers.put(EdayunConstants.HEADER_TOKEN, requireText(token, "edayun requires a token")); headers.put(EdayunConstants.HEADER_APP_KEY, requireText(appKey, "edayun requires ChannelSecret.appKey()")); headers.put(EdayunConstants.HEADER_TIMESTAMP, String.valueOf(timestamp)); - headers.put(EdayunConstants.HEADER_SIGN, sign(bodyJson, appSecret)); + headers.put(EdayunConstants.HEADER_SIGN, signValue); HttpRequest.Builder builder = HttpRequest.builder() .method(method) @@ -94,6 +101,20 @@ public final class EdayunRequestFactory { } /** + * 业务请求(测试环境免签版本)。 + * + *

文档说明:测试环境可把 {@code openApiAdminSecret} 直接当 {@code sign} 传, + * 不必计算 MD5。生产环境请用带 {@code appSecret} 的重载。

+ */ + public HttpRequest businessUnsigned(String baseUrl, String path, String method, + String token, String appKey, String openApiAdminSecret, + long timestamp, String bodyJson, String operation) { + return buildBusiness(baseUrl, path, method, token, appKey, + requireText(openApiAdminSecret, "edayun requires openApiAdminSecret"), timestamp, + bodyJson, operation); + } + + /** * 签名:{@code MD5(body + "." + appSecret)},小写十六进制。 * *

注意是 body 原文参与签名——所以业务方法要把「即将发出的那个字符串」 diff --git a/warehouse-sdk-edayun/src/test/java/common/ecommerce/edayun/probe/EdayunTokenProbeTest.java b/warehouse-sdk-edayun/src/test/java/common/ecommerce/edayun/probe/EdayunTokenProbeTest.java new file mode 100644 index 0000000..53a4706 --- /dev/null +++ b/warehouse-sdk-edayun/src/test/java/common/ecommerce/edayun/probe/EdayunTokenProbeTest.java @@ -0,0 +1,100 @@ +package common.ecommerce.edayun.probe; + +import common.ecommerce.edayun.EdayunConstants; +import common.ecommerce.edayun.auth.EdayunTokenClient; +import common.ecommerce.edayun.http.EdayunRequestFactory; +import common.ecommerce.http.JacksonJsonCodec; +import common.ecommerce.http.OkHttpTransport; +import common.ecommerce.shared.error.ChannelException; +import common.ecommerce.shared.spi.AccessToken; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; + +import java.time.Clock; + +/** + * 易达云获取 token 的联调探针。 + * + *

默认跳过,用 {@code -Dedayun.probe=true} 开启;凭据走系统属性, + * 真实值不入库:

+ * + *
+ * mvn -pl warehouse-sdk-edayun -DfailIfNoTests=false \
+ *     -Dedayun.probe=true \
+ *     -Dedayun.probe.baseUrl=https://test.omniselling.com.cn/saas-service/openapi/microService \
+ *     -Dedayun.probe.clientId=你的OMS授权用户名 \
+ *     -Dedayun.probe.clientSecret=你的OMS授权密码 \
+ *     -Dtest=EdayunTokenProbeTest test
+ * 
+ * + *

只调只读的 token 接口,不碰任何业务写接口。异常只打印不抛出, + * 便于一次跑完对照真实报文。

+ */ +@EnabledIfSystemProperty(named = "edayun.probe", matches = "true") +class EdayunTokenProbeTest { + + private static final String BASE_URL = opt("edayun.probe.baseUrl", EdayunConstants.PR_BASE_URL); + private static final String CLIENT_ID = System.getProperty("edayun.probe.clientId", ""); + private static final String CLIENT_SECRET = System.getProperty("edayun.probe.clientSecret", ""); + + @Test + void probeToken() { + Assumptions.assumeTrue(!CLIENT_ID.trim().isEmpty() && !CLIENT_SECRET.trim().isEmpty(), + "未提供 -Dedayun.probe.clientId / clientSecret,跳过"); + + System.out.println("\n================ edayun / POST /oauth/token ================"); + System.out.println("[probe] baseUrl=" + BASE_URL + + " clientId=" + mask(CLIENT_ID) + " clientSecret=" + mask(CLIENT_SECRET)); + + EdayunTokenClient client = new EdayunTokenClient( + new OkHttpTransport("common-ecommerce/1.0.0 (edayun-probe)"), + new JacksonJsonCodec(), Clock.systemUTC()); + + try { + AccessToken token = client.fetch(BASE_URL, CLIENT_ID, CLIENT_SECRET); + System.out.println("[OK] token=" + mask(token.value()) + + " tokenType=" + token.tokenType() + + " expiresAt=" + token.expiresAt() + + " refreshToken=" + (token.refreshToken() == null ? "null" : "present")); + System.out.println(" 有效期剩余(秒)=" + + (token.expiresAt().getEpochSecond() - java.time.Instant.now().getEpochSecond())); + } catch (RuntimeException e) { + String msg = e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage(); + System.out.println("[ERROR] " + e.getClass().getSimpleName() + ": " + + (msg.length() <= 600 ? msg : msg.substring(0, 600) + "…")); + if (e instanceof ChannelException) { + ChannelException ce = (ChannelException) e; + System.out.println(" platformCode=" + ce.platformCode() + + " retryable=" + ce.retryable()); + } + } + } + + /** 签名算法自检:不联网,确认 MD5(body + "." + appSecret) 的低 8 位形态。 */ + @Test + void probeSignatureShape() { + String secret = System.getProperty("edayun.probe.appSecret", ""); + Assumptions.assumeTrue(!secret.trim().isEmpty(), + "未提供 -Dedayun.probe.appSecret,跳过签名自检"); + String sign = EdayunRequestFactory.sign("{\"a\":1}", secret); + System.out.println("[sign] MD5(body + \".\" + appSecret) = " + sign + + " (长度应为 32 的小写十六进制)"); + } + + private static String mask(String v) { + if (v == null || v.length() <= 6) { + return "***"; + } + return v.substring(0, 4) + "***" + v.substring(v.length() - 2); + } + + private static String opt(String name, String fallback) { + String value = System.getProperty(name, ""); + if (value == null) { + return fallback; + } + String trimmed = value.trim(); + return trimmed.isEmpty() ? fallback : trimmed; + } +}