微博开放平台 auth2.0

2022/6/11 23:54:20

本文主要是介绍微博开放平台 auth2.0,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

微博开放平台 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;
    }

 

 



这篇关于微博开放平台 auth2.0的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程