software.amazon.spapi 官方提供的sdk如何使用已知的accessToken?
官方 SDK 并不强制你用 refresh_token 去换 token——如果你手里已经有一个仍然有效的 LWA access_token,只要把它放进请求头 x-amz-access-token 即可;这是官方文档现在推荐的做法(最近文档里甚至给了“不带签名、只带 header”的示例)
下面给你两条常见路径,按需选:
curl "https://sellingpartnerapi-na.amazon.com/sellers/v1/marketplaceParticipations" \
-H "x-amz-access-token: Atza|<你的token>" \
-H "user-agent: MyApp/1.0 (Language=CLI)" \
-H "x-amz-date: 20250101T120000Z"官方“Connect to the SP-API”页面明确列出要加的头,示例也展示了不带签名、只带 x-amz-access-token 的请求。注意 token 有效期约 1 小时。不同语言的官方 SDK 都是基于 OpenAPI 生成的客户端,底层一般都有“默认请求头/拦截器”的注入点。思路是:把 x-amz-access-token 当成普通自定义头塞进去即可。
import okhttp3.*;
import com.amazon.spapi.client.*; // 你的生成包名可能不同
import com.amazon.spapi.api.SellersApi;
String accessToken = "Atza|...";
ApiClient client = new ApiClient();
OkHttpClient http = client.getHttpClient().newBuilder()
.addInterceptor(chain -> {
Request req = chain.request().newBuilder()
.header("x-amz-access-token", accessToken)
.header("user-agent", "MyApp/1.0 (Language=Java)")
.build();
return chain.proceed(req);
}).build();
client.setHttpClient(http);
client.setBasePath("https://sellingpartnerapi-na.amazon.com");
SellersApi api = new SellersApi(client);
var res = api.getMarketplaceParticipations();官方 Java 教程默认演示用 refresh_token 让 SDK 自己换 token;但如果你已持有 access_token,也可以像上面这样直接注入 header 使用。(developer-docs.amazon.com)
using Amazon.SellingPartnerAPIAA.Client; // 你的命名空间可能不同
using Amazon.SellingPartnerAPIAA.Api;
var cfg = new Configuration { BasePath = "https://sellingpartnerapi-na.amazon.com" };
cfg.AddDefaultHeader("x-amz-access-token", "Atza|...");
cfg.UserAgent = "MyApp/1.0 (Language=C#)";
var api = new SellersApi(cfg);
var res = api.GetMarketplaceParticipations();C# 的生成客户端通常支持在Configuration/RestClient上添加默认头;把 token 放进x-amz-access-token即可。官方“生产调用”页面也列出必需的几个头部。
from spapi_client import Configuration, ApiClient, SellersApi # 你的包名/类名可能不同
cfg = Configuration()
cfg.host = "https://sellingpartnerapi-na.amazon.com"
api_client = ApiClient(cfg)
api_client.set_default_header("x-amz-access-token", "Atza|...")
api_client.set_default_header("user-agent", "MyApp/1.0 (Language=Python)")
api = SellersApi(api_client)
res = api.get_marketplace_participations()Python 的生成客户端一般提供set_default_header或可直接修改default_headers添加自定义头。
const r = await fetch(
"https://sellingpartnerapi-na.amazon.com/sellers/v1/marketplaceParticipations",
{
headers: {
"x-amz-access-token": "Atza|...",
"user-agent": "MyApp/1.0 (Language=Node)",
"x-amz-date": "20250101T120000Z",
},
}
);
const data = await r.json();官方文档与 Postman 教程都明确需要把access_token以x-amz-access-token传入(有时文档里也能看到旧写法x-amzn-access-token,本质一致)。
x-amz-access-token 头里。
我使用了你的方法 还是报错
Cannot invoke "com.amazon.SellingPartnerAPIAA.LWAAuthorizationSigner.sign(okhttp3.Request)" because "this.lwaAuthorizationSigner" is null