美文网首页
Java微博授权第三方登陆

Java微博授权第三方登陆

作者: Felix_Fang | 来源:发表于2019-01-18 21:41 被阅读0次

微博开放平台
创建应用获取APPkey和App Secret

APPkey和App Secret
开发文档
开发文档
首先需要重定向到第一个路径:
https://api.weibo.com/oauth2/authorize?
client_id=你的APPKEY
&response_type=code&
redirect_uri=你的回调地址

通过回调地址返回的code值访问第二个路径:
https://api.weibo.com/oauth2/access_token?
client_id=你的APPKEY
&client_secret=你的App Secret
&grant_type=authorization_code
&redirect_uri=回调地址
&code=返回的code值

这里会返回accessToken、uid、expires_in、isRealName、remind_in

我们需要accessToken、uid访问下面这个地址:
"https://api.weibo.com/2/users/show.json?
access_token= 上面的accessToken&
uid= 上面的uid

这里会返回需要的所有信息,这也太多了,什么家底都出来了
官方PAI说明

官方API

未上线的应用测试需要添加测试人员

测试人员

代码部分(摘要)

String params = "client_id="+   WeiboConstant.APP_ID
                    + "&client_secret=" +   WeiboConstant.APP_SECRET
                    + "&grant_type="    +   "authorization_code"
                    + "&redirect_uri="  +   WeiboConstant.REDIRECT_URI
                    + "&code="          +   code;
            
            // 用code换取accessToken
            String result = HttpsUtil.post("https://api.weibo.com/oauth2/access_token", params);
            @SuppressWarnings("unchecked")
            Map<String, Object> map = JSONUtils.toHashMap(result);
            
            String access_token =  (String) map.get("access_token");
            String uid =  (String) map.get("uid");// 这个uid就是微博用户的唯一用户ID
            Integer expires_in =  (Integer) map.get("expires_in");// 有效期,单位秒
            /*Boolean isRealName =  map.get("isRealName");
            Integer remind_in =  map.get("remind_in");*/
            
            // 用uid和accessToken换取用户信息
            String resultUser = HttpsUtil.get(
                                "https://api.weibo.com/2/users/show.json?access_token="+access_token
                                +"&uid="+uid);
            @SuppressWarnings("unchecked")
            Map<String, Object> mapUser = JSONUtils.toHashMap(resultUser);
            
            Long id = (Long) mapUser.get("id"); //用户UID
            String idstr = (String) mapUser.get("idstr"); //字符串型的用户UID
            String screen_name = (String) mapUser.get("screen_name"); //昵称
            String profile_image_url = (String) mapUser.get("profile_image_url");//头像   
            String location = (String) mapUser.get("location"); //地址
            String city = (String) mapUser.get("city");//"city":"3",
            String description = (String) mapUser.get("description");//简介

相关文章

网友评论

      本文标题:Java微博授权第三方登陆

      本文链接:https://www.haomeiwen.com/subject/woeodqtx.html