如果您的应用和淘宝开放平台对接后,需要获取用户隐私信息(如:商品、订单、收藏夹等),为保证用户数据的安全性与隐私性,您的应用需要取得用户的授权。在这种情况下,您的应用需要引导用户完成“使用淘宝帐号登录并授权”的流程。
授权文档:http://open.taobao.com/doc/detail.htm?spm=a219a.7386781.1998343697.1.iitwSA&id=118
2.根据授权后回调用code换取access_token
使用第三方fastJson.jar,淘宝开放sdk.jar
/**
* @param access_code 授权登陆后的code
*/
public void get_access_token(String access_code) throws Exception {
String url = "https://oauth.taobao.com/token";
Map<String, String> props = new HashMap<String, String>();
props.put("grant_type", "authorization_code");
props.put("code", access_code);
props.put("client_id", APIIConst.APP_KEY);
props.put("client_secret", APIIConst.APP_SECRET);
props.put("redirect_uri", APIIConst.REQ_BACK_URL);
props.put("view", "web");
String json_text = WebUtils.doPost(url, props, 30000, 30000);
JSONObject obj = (JSONObject) JSON.parse(json_text);
// access_token
obj.get("access_token").toString();
// Access token的类型目前只支持bearer
obj.get("token_type").toString();
// Access token过期时间
obj.get("expires_in").toString();
// Refresh token
obj.get("refresh_token").toString();
// Refresh token过期时间
obj.get("re_expires_in").toString();
// r1级别API或字段的访问过期时间
obj.get("r1_expires_in").toString();
// r2级别API或字段的访问过期时间
obj.get("r2_expires_in").toString();
// w1级别API或字段的访问过期时间
obj.get("w1_expires_in").toString();
// w2级别API或字段的访问过期时间
obj.get("w2_expires_in").toString();
// 淘宝账号
URLDecoder.decode(obj.get("taobao_user_nick").toString(), "UTF-8");
// 淘宝帐号对应id
obj.get("taobao_user_id").toString();
}
导入淘宝sdk的包如下:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.internal.util.WebUtils;
import com.taobao.api.request.TbkScMaterialOptionalRequest;
import com.taobao.api.response.TbkScMaterialOptionalResponse;
接口文档:http://open.taobao.com/doc.htm?docId=118&docType=1
http://www.xiaojianjian.net/archives/253