需求:获取京东商家的订单
环境:java,官方宙斯SDK
1.申请账号注册开发者创建应用参考官方文档注册
注意一点是回调页面url:地址为用户访问应用的地址,当用户访问您的应用时,jos会把相关授权参数通过这个回调地址传给您;这个回调url只是在获取商家授权时需要用到,如果不是后台方法获取的话,填www.baidu.com就好。
2.获取appKey,appSecret,accessToken,下载SDK
感谢前辈提供帮助
在控制台应用管理中,从应用证书中可以找到appKey,appSecret;
点击SDK下载,获得SDK;
在应用基本信息中点击测试;
用商家账号登陆,点击授权后,跳转到百度首页,在地址栏中可看到后面的带参?code=48OtVB,保存起来;
拼接https://oauth.jd.com/oauth/token?grant_type=authorization_code&client_id=你的appkey&redirect_uri=http:www.baidu.com&code=48OtVB&state=1212&client_secret=你的appSecret
用浏览器打开;
{
“access_token”: “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”,
“code”: 0,
“expires_in”: 86390,
“refresh_token”: “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”,
“time”: “1516248547902”,
“token_type”: “bearer”,
“uid”: “524625331”,
“user_nick”: “jd abcd”
}
注意,测试的accress_token的时效性只有24小时,之后需要重新获取一次
3.后台获取订单
官方java示例:
public JdClient client=new DefaultJdClient(SERVER_URL,accessToken,appKey,appSecret);
OrderSearchRequest request=new OrderSearchRequest();
request.setStartDate( "jingdong" );
request.setEndDate( "jingdong" );
request.setOrderState( "jingdong" );
request.setPage( "jingdong" );
request.setPageSize( "jingdong" );
request.setOptionalFields( "jingdong" );
request.setSortType( "jingdong" );
request.setDateType( "jingdong" );
OrderSearchResponse response=client.execute(request);
项目中引入宙斯SDK后,测试了下,会报错
只有open-api-sdk-2.0会报错java.lang.NoSuchMethodError,还得另外的jar包。
这里就超级坑了,翻遍了官方文档,百度了很久,终于凑齐了jar包
jar下载
最后就是response的解析了,jos使用的是分页查询,注意是把Page理解为页码,官方描述容易误导,说一次最多可查1000个订单,最大PageSize为100,最大Page为50,容易理解为一次查询Page*PageSize个订单,其实是结果以PageSize分页,返回的只有第Page页的订单。
结尾贴一下自己的代码,大神请无视
package com.sat.jos;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.JdException;
import com.jd.open.api.sdk.domain.order.OrderResult;
import com.jd.open.api.sdk.domain.order.OrderSearchInfo;
import com.jd.open.api.sdk.request.order.OrderSearchRequest;
import com.jd.open.api.sdk.response.order.OrderSearchResponse;
import com.sat.entity.Result_Order;
import com.sat.util.PropertiesConfig;
/**
* 京东jos调用类
* @author VULCAN
*
*/
public class Jos {
private String SERVER_URL=PropertiesConfig.readData("josapi.properties", "SERVER_URL");
private String accessToken=PropertiesConfig.readData("josapi.properties", "accessToken");
private String appKey=PropertiesConfig.readData("josapi.properties", "appKey");
private String appSecret=PropertiesConfig.readData("josapi.properties", "appSecret");
private String setOrderState=PropertiesConfig.readData("josapi.properties", "setOrderState");
private String page=PropertiesConfig.readData("josapi.properties", "page");
private String page_size=PropertiesConfig.readData("josapi.properties", "page_size");
/**
* 发送请求获取响应
* @param MySERVER_URL
* @param MyaccessToken
* @param MyappKey
* @param MyappSecret
* @param MystarDate
* @param MyendDate
* @param MysetOrderState
* @param Mypage
* @param Mypage_size
* @return
*/
public OrderSearchResponse getOrderSearchResponse(String MySERVER_URL,String MyaccessToken,String MyappKey,String MyappSecret,String MystarDate,
String MyendDate,String MysetOrderState,String Mypage,String Mypage_size){
//创建请求
JdClient client=new DefaultJdClient(MySERVER_URL,MyaccessToken,MyappKey,MyappSecret);
OrderSearchRequest request=new OrderSearchRequest();
//开始时间
request.setStartDate(MystarDate);
//结束时间
request.setEndDate(MyendDate);
//订单状态
request.setOrderState(MysetOrderState);
//查询的页数
request.setPage(Mypage);
//每页的条数(最大page_size 100条)
request.setPageSize(Mypage_size);
try {
OrderSearchResponse response=client.execute(request);
return response;
} catch (JdException e) {
e.printStackTrace();
}
return null;
}
/**
* 根据时间获取未出库订单
* @param endDate 结束时间
* @param minute 时间间隔分钟
* @return
*/
public Result_Order getOrderByTime(String endDate,int minute){
String orderState =setOrderState;
return this.getOrderByTime(endDate, minute, orderState);
}
/**
* 根据时间、状态获取订单
* @param endDate
* @param minute
* @return
*/
public Result_Order getOrderByTime(String endDate,int minute,String orderState){
Result_Order result_order=new Result_Order();
List finalorderlist = new ArrayList();
//时间处理
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String starDate="";
if(!endDate.equals("")){
try {
Date end =sdf.parse(endDate);
cal.setTime(end);
cal.add(Calendar.MINUTE, 0-minute);
starDate=sdf.format(cal.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
}
//封装返回结果
OrderSearchResponse response=this.getOrderSearchResponse(SERVER_URL, accessToken, appKey, appSecret,
starDate, endDate, orderState, page, page_size);
String code = response.getCode();
result_order.setCode(code);
String zhDesc = response.getZhDesc();
result_order.setZhDesc(zhDesc);
OrderResult or =response.getOrderInfoResult();
int orderTotal = or.getOrderTotal();
result_order.setOrderTotal(orderTotal);
List orderlist = or.getOrderInfoList();
finalorderlist.addAll(orderlist);
int pagesize = Integer.parseInt(page_size);//每页订单数
//多页时多次请求
if(orderTotal>pagesize){
int pages =1;//总页数
if(orderTotal%pagesize>0){
pages =orderTotal/pagesize+1;
}else{
pages =orderTotal/pagesize;
}
for(int newpage=Integer.parseInt(page)+1;newpage<=pages;newpage++){
String newpageString =newpage+"";
response=this.getOrderSearchResponse(SERVER_URL, accessToken, appKey, appSecret,
starDate, endDate, orderState, newpageString, page_size);
or =response.getOrderInfoResult();
orderlist = or.getOrderInfoList();
finalorderlist.addAll(orderlist);
}
}
result_order.setOrderlist(finalorderlist);
return result_order;
}
}