JAVA接入淘宝授权API
- 工作小总结
- 时间:2019-02-12 09:49
- 3581人已阅读
🔔🔔好消息!好消息!🔔🔔
如果您需要注册ChatGPT,想要升级ChatGPT4。凯哥可以代注册ChatGPT账号,代升级ChatGPT4
有需要的朋友👉:微信号
如果您的应用和淘宝开放平台对接后,需要获取用户隐私信息(如:商品、订单、收藏夹等),为保证用户数据的安全性与隐私性,您的应用需要取得用户的授权。在这种情况下,您的应用需要引导用户完成“使用淘宝帐号登录并授权”的流程。
授权文档:http://open.taobao.com/doc/detail.htm?spm=a219a.7386781.1998343697.1.iitwSA&id=118
访问 https://oauth.taobao.com/authorize?response_type=code&client_id=淘宝APPID&redirect_uri=后台设置的回调地址
如凯哥的:
https://oauth.taobao.com/authorize?response_type=code&client_id=123456&state=155&redirect_uri=http://weixin.frp.kaigejava.com/tbSessionKeyReturnUrl/
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
上一篇: 记一次博客被群压的经历
下一篇: JAVA接入京东授权API