ebay的api的认证及开发(java语言)二

1.说下如何通过rest的方式进行授权,这个也是ebay官方提供的最新的api,一般sdk更新会比较慢。
首先在百度上搜索ebay-client,github或者码云都有对应的信息ebay-client

<dependency>
    <groupId>com.ebay.auth</groupId>
    <artifactId>ebay-oauth-java-client</artifactId>
  <version>1.1.0</version>
</dependency>

将这个jar包导入项目,下面就可以进行授权了,ebayclient所有的授权都是通过OAuth2Api 这个类来完成的。
(1)、首先增加在resource目录下增加一个ebay-config.yaml配置文件
ebay配置文件
然后在配置文件中按照固定格式填写开发者信息这样能够ebay-client才能够正确的识别;

name: ebay-config

# Trading API OAuth - https://developer.ebay.com/api-docs/static/oauth-tokens.html

# UPDATE the values in this file, before running the tests
api.sandbox.ebay.com:
  appid: 
  certid: 
  devid: 
  redirecturi: 

api.ebay.com:
  appid: 
  certid: 
  devid: 
  redirecturi: 

这个配置是兼容生产环境和沙盒环境,只需要在开发代码的时候进行切换即可
(2)进行url的拼接

首先需要加载到配置文件的信息

 static {
        try {
            CredentialUtil.load(new FileInputStream(ResourceUtils.getFile("classpath:ebay-config.yaml")));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
 @Override
    public String getAuthorizaRestfulUrl(String shopName) {
        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.finances");
        scopeList.add("https://api.ebay.com/oauth/api_scope/sell.fulfillment");
        scopeList.add("https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly");
        
        String url= oauth2Api.generateUserAuthorizationUrl(Environment.PRODUCTION,scopeList, Optional.of(shopName));
        return url;
    }

scopeList 是ebay的授权链接,通过这些链接来确定可以访问到哪些api的信息;这个可以查看api文档,每个api都会标价出需要的授权链接;Environment.PRODUCTION这个则是切换生产及沙盒环境的;Optional.of(shopName)则是回调时是否带上的参数,因为我这里是需要授权多个店铺的所有需要区分下店铺;这个参数是可选的,如果不要回调时带上这个参数则可以传空值即可;
(3)与上篇相同的
通过(2)的url拼接,我们可访问到授权页面,手动授权成功后ebay会进行回调,回调时会返回一个code值;我们通过这个code访问ebay就可以获取到token(注意:这个code值是有时长限制的,五分钟之后就会过期,需要重新授权获取)

 @Override
    public String gettokenByCode(String code, String state) throws IOException {
        OAuth2Api oauth2Api = new OAuth2Api();
        OAuthResponse oAuthResponse = oauth2Api.exchangeCodeForAccessToken(Environment.PRODUCTION,code);
        AccessToken accessToken = oAuthResponse.getAccessToken().get();
        String token = accessToken.getToken();
        String refreshToken=oAuthResponse.getRefreshToken().get().getToken();
        Date expiresOn = oAuthResponse.getRefreshToken().get().getExpiresOn();
        return token;
    }

code为ebay回调时返回的数据,state则是上一步我们手动添加的收据,会原封不动的返回来,调用成功后,会返回token及refreshToken,token只有两个小时的时限,超时之后我们可以通过refreshToKen去重新获取,refreshToKen是永久不过期的
(4)、我们就需要使用refreshToken去定时的获取新的token,保证ebay的链接是通畅的;

 @Override
    public void reRefreshToken(String freshToken) throws IOException {
                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.finances");
                 scopeList.add("https://api.ebay.com/oauth/api_scope/sell.fulfillment");
                 scopeList.add("https://api.ebay.com/oauth/api_scope/sell.fulfillment.readonly");
                OAuthResponse oAuthResponse = null;
                try {
                    oAuthResponse = oauth2Api.getAccessToken(Environment.PRODUCTION,freshToken,scopeList);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                AccessToken accessToken = oAuthResponse.getAccessToken().get();
            }
(5).以上就完成了授权任务,可以调用api去获取相关的信息了;
 public void getOrders(String restfulToken ) throws IOException{
      
           
          
            Integer endNumber=3;
            String endTime = getISO8601Timestamp(-endNumber);
            String beginTime = getISO8601Timestamp(-beginNumber);

            final OkHttpClient okHttpClient = OkHttpUtils.getInstance();
            final Request request = new Request.Builder()
                    .url(EBayEnvironment.resuFulUrl+"?filter=creationdate:%5B"+beginTime+".."+endTime+"%5D&limit=50&offset="+offSet)
                    .get()
                    .addHeader("Authorization", "Bearer " + restfulToken)
                    .build();
            final Response response = okHttpClient.newCall(request).execute();
			System.out.println( response.body().string())
        }
 /**
     * 传入Data类型日期,返回字符串类型时间(ISO8601标准时间)
     * @param
     * @return
     */
    public static String getISO8601Timestamp(Integer days){

        Date now = new Date();
        Date startDate = DateUtils.addDays(now, days);
        TimeZone tz = TimeZone.getTimeZone("UTC");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        df.setTimeZone(tz);
        String nowAsISO = df.format(startDate);
        return nowAsISO;
    }

这样就可以获取到订单信息了;

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值