对接eBay流程(demo可直接运行)

eBay对接流程

首先,你必须要有个eBay的开发者账号,然后使用开发者账号的信息去跳转到eBay的授权页面,拿到授权code,之后,你就可以随意控制卖家的所有操作了。

下面详细的说下操作流程:

第一步:

在这里插入图片描述

第二步:

点击上图的”User Tokens“,

在这里插入图片描述

第三步:

点击上图中的Get a Token from eBay via Your Application,然后填写上你的https的回调地址;如图

在这里插入图片描述

第四步:

以上准备设置完之后,就开始进入对接阶段了。

首先,你得弄明白这张图:

在这里插入图片描述

这个图表达的意思是:先从应用端请求eBay授权服务,跳转到eBay授权页面,由卖家登录后得到授权码(Authorization code),再使用这个授权码去请求eBay的接口获取token,最后,用这个token去调用eBay的api接口;

具体流程参考官网文档:https://developer.ebay.com/api-docs/static/oauth-authorization-code-grant.html

第一步:获取eBay授权页面的地址

@GetMapping("getAuthUrl")
public Mono<String> getAuthUrl() {
    OAuth2Api oauth2Api = new OAuth2Api();
    List<String> scopeList = new ArrayList<>();
    scopeList.add("https://api.ebay.com/oauth/api_scope");
    scopeList.add("https://api.ebay.com/oauth/api_scope/sell.fulfillment");
    scopeList.add("https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly");
    String authorization_url = oauth2Api.generateUserAuthorizationUrl(EBayEnvironment.PRODUCTION, scopeList, Optional.empty());
    return Mono.just(authorization_url);
}

第二步:根据授权url地址响应的code,去eBay获取token

@GetMapping("getToken/{code}")
public Mono<OAuthResponse> getToken(@PathVariable("code") final String code) throws IOException {
    OAuth2Api oauth2Api = new OAuth2Api();
    OAuthResponse oauth2Response = oauth2Api.exchangeCodeForAccessToken(EBayEnvironment.PRODUCTION, code);
    return Mono.just(oauth2Response);
}

第三步:根据token获取订单数据

@GetMapping("getOrders/{token}")
public Mono<Response> getOrders(@PathVariable("token") final String token) throws IOException {
    final OkHttpClient okHttpClient = new OkHttpClient();
    final Request request = new Request.Builder()
            .url("https://api.ebay.com/sell/fulfillment/v1/order")
            .get()
            .addHeader("Authorization", "Bearer " + token)
            .build();
    final Response response = okHttpClient.newCall(request).execute();
    return Mono.just(response);
}

demo地址:https://github.com/GoldWater16/eBay-demo

发布了222 篇原创文章 · 获赞 20 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/qq_17555933/article/details/98966955