ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

微博开放平台 auth2.0

2022-06-11 18:32:32  阅读:194  来源: 互联网

标签:body code String auth2.0 token entity 微博 vo 开放平台


微博开放平台 https://open.weibo.com/
创建应用

 

 

 

 

 

 

 

 然后根据code信息 我们可以去授权服务器获取对应的token信息  获取token信息只支持post 方式提交  我们可以使用postman 方式提交

https://api.weibo.com/oauth2/access_token?client_id=1093598037&client_secret=1085c8de04dee49e9bb110eaf2d3cf62&grant_type=authorization_code&redirect_uri=http://msb.auth.com/success.html&code=59d62e59e5ead5a4ea89c6f9cf212568

 

 获取到了token信息后我们就可以去资源服务器获取资源信息

 

 我们可以调用userShow

 

 

 根据之前获取的access_token 和 uid 来获取对应的user信息

 

 

 

 

 

 

 

 在后台服务获取code  并获取对应的token信息

 

@Controller
public class OAuth2Controller {
    
    @RequestMapping("/oauth/weibo/success")
    public String weiboOAuth(@RequestParam("code") String code) throws Exception {
        /*微博会回调接口 需要传入对应的参数  token*/
        Map<String,String> body = new HashMap<>();
        body.put("client_id","1093598037");
        body.put("client_secret","1085c8de04dee49e9bb110eaf2d3cf62");
        body.put("grant_type","authorization_code");
        body.put("redirect_uri","http://msb.auth.com/oauth/weibo/success");
        body.put("code",code);
        //根据code 获取对应的token信息
        HttpResponse post = HttpUtils.doPost("https://api.weibo.com"
                , "/oauth2/access_token"
                , "post"
                , new HashMap<>()
                , null
                , body
        );
        int statusCode = post.getStatusLine().getStatusCode();
        if(statusCode != 200){
            // 说明获取Token失败,就调回到登录页面
            return "redirect:http://msb.auth.com/login.html";
        }
        // 说明获取Token信息成功
        
        String json = EntityUtils.toString(post.getEntity());
        SocialUser socialUser = JSON.parseObject(json, SocialUser.class);
        // 注册成功就需要调整到商城的首页
        return "redirect:http://msb.mall.com/home.html";
        
        
    }
}
这里的SocialUser  就是为了接收微博传过来的参数格式而创建的类
@Data public class SocialUser { private String accessToken;//token 信息 private long remindIn; private long expiresIn;//过期时间 private String uid;//用户识别编号 private boolean isRealName; }

此时我们需要远程调用 member服务去后端查看  是否是第一次登陆  如果是 需要注册 将微博返回的信息和注册信息一并存入数据库中

 

 

 

 

 

@Controller
public class OAuth2Controller {
    
    @Autowired
    private MemberFeginService memberFeginServicel;
    
    @RequestMapping("/oauth/weibo/success")
    public String weiboOAuth(@RequestParam("code") String code) throws Exception {
        /*微博会回调接口 需要传入对应的参数  token*/
        Map<String,String> body = new HashMap<>();
        body.put("client_id","1093598037");
        body.put("client_secret","1085c8de04dee49e9bb110eaf2d3cf62");
        body.put("grant_type","authorization_code");
        body.put("redirect_uri","http://msb.auth.com/oauth/weibo/success");
        body.put("code",code);
        //根据code 获取对应的token信息
        HttpResponse post = HttpUtils.doPost("https://api.weibo.com"
                , "/oauth2/access_token"
                , "post"
                , new HashMap<>()
                , null
                , body
        );
        int statusCode = post.getStatusLine().getStatusCode();
        if(statusCode != 200){
            // 说明获取Token失败,就调回到登录页面
            return "redirect:http://msb.auth.com/login.html";
        }
        // 说明获取Token信息成功
        
        String json = EntityUtils.toString(post.getEntity());
        SocialUser socialUser = JSON.parseObject(json, SocialUser.class);
        R r = memberFeginServicel.socialLogin(socialUser);
        if(r.getCode()!=0){
            //表示登陆错误
            return "redirect:http://msb.auth.com/login.html";
        }
        String entityJson = (String)r.get("entity");
        System.out.println("------------->"+entityJson);
        // 注册成功就需要调整到商城的首页
        return "redirect:http://msb.mall.com";
        
        
    }
}
@Data
public class SocialUser {
    private String accessToken;//token 信息
    private long remindIn;
    private long expiresIn;//过期时间
    private String uid;//用户识别编号
    private boolean isRealName;
}

 

 

    @RequestMapping("/oauth2/login")
    public R socialLogin(@RequestBody SocialUser vo){
        System.out.println(vo);
        MemberEntity entity=memberService.login(vo);
        return R.ok().put("entity", JSON.toJSONString(entity));
    }
  @Override
    public MemberEntity login(SocialUser vo) {
       
        // 如果不是第一次社交登陆 登陆功能
        MemberEntity memberEntity = this.getOne(new QueryWrapper<MemberEntity>().eq("social_uid", vo.getUid()));
        if(memberEntity!=null){
            //说明 当前用户已经注册过了 更新token和过期时间
            MemberEntity entity=new MemberEntity();
            entity.setId(memberEntity.getId());
            entity.setAccessToken(vo.getAccessToken());
            entity.setExpiresIn(vo.getExpiresIn());
            this.updateById(entity);
            //在返回的登陆用户信息中我们同步的返回更新后的token和过期时间
             memberEntity.setAccessToken(vo.getAccessToken());
             memberEntity.setExpiresIn(vo.getExpiresIn());
            return memberEntity;
        }
        
        //如果该用户是第一次社交登陆  那么需要注册  
        MemberEntity entity=new MemberEntity();
        entity.setAccessToken(vo.getAccessToken());
        entity.setExpiresIn(vo.getExpiresIn());
        entity.setUid(vo.getUid());
        //通过token 调用微博开放接口来获取用户的基本信息
        try {
            Map<String,String> querys = new HashMap<>();
            querys.put("access_token",vo.getAccessToken());
            querys.put("uid",vo.getUid());
            HttpResponse response = HttpUtils.doGet("https://api.weibo.com"
                    , "/2/users/show.json"
                    , "get"
                    , new HashMap<>()
                    , querys
            );
            if(response.getStatusLine().getStatusCode() == 200){
                //将对象装换为字符串
                String json = EntityUtils.toString(response.getEntity());
                System.out.println("将对象装换为字符串"+json);
                //将字符串装换为对象
                JSONObject jsonObject = JSON.parseObject(json);
                System.out.println("将字符串装换为对象"+jsonObject);
                String nickName = jsonObject.getString("screen_name");
                String gender = jsonObject.getString("gender");
                entity.setNickname(nickName);
                entity.setGender("m".equals(gender)?1:0);
            }
        }catch (Exception e){
    
        }
        // 注册用户信息
        this.save(entity);
        return entity;
    }

 

 

标签:body,code,String,auth2.0,token,entity,微博,vo,开放平台
来源: https://www.cnblogs.com/Lcch/p/16366479.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有