微博API登录(第三方登录)帮朋友推荐

微博API登录(第三方登录)帮朋友推荐

林裕帆 2024-10-18 华人 79 次浏览 0个评论


@Slf4j
@Controller
public class WeiBoLoginController {

   @Value("${weibo.client_id}")
   private String clientId;

   @Value("${weibo.secret}")
   private String secret;

   @Value("${weibo.redirect}")
   private String redirect;

   @RequestMapping("/weibo")
   public String weiBo(String code, Map<String, Object> map) {
       String url = "https://api.weibo.com/oauth2/access_token?client_id=" + clientId + "&client_secret=" + secret + "&grant_type=authorization_code&redirect_uri=" + redirect + "&code=" + code;
       log.info("accessTokenUrl = {}", url);

       String resp = HttpUtil.post(url, "");
       log.info("accessTokenResp = {}", resp);

       JSON json = JSONUtil.parse(resp);
       String token = json.getByPath("access_token").toString();
       String uid = json.getByPath("uid").toString();
       log.info("token = {}", token);
       log.info("uid = {}", uid);

       String userInfoUrl = "https://api.weibo.com/2/users/show.json?uid=" + uid + "&access_token=" + token;
       String userInfo = HttpUtil.get(userInfoUrl);
       log.info("userInfo = {}", userInfo);
       JSON info = JSONUtil.parse(userInfo);
       map.put("uId", uid);
       map.put("name", info.getByPath("name"));
       map.put("imgUrl", info.getByPath("profile_image_url"));
       return "success";
   }
}

微博API登录(第三方登录)帮朋友推荐

# 应用ID
weibo.client_id=1556869902
# 密钥
weibo.secret=ddbf4544bf8856ffcd2e7d97761eb33f
# 回调地址
weibo.redirect=http://xuewei.world/test/weibo

https://api.weibo.com/oauth2/authorize?client_id=你的应用ID&response_type=code&redirect_uri=你的回调地址

微博API登录(第三方登录)帮朋友推荐

<a href=https://www.bilibili.com/read/mobile/"https://api.weibo.com/oauth2/authorize?client_id=1556869902&response_type=code&redirect_uri=http://xuewei.world/test/weibo"></a>


微博API登录(第三方登录)帮朋友推荐

转载请注明来自宁波良居装饰工程有限公司,本文标题:《微博API登录(第三方登录)帮朋友推荐》

百度分享代码,如果开启HTTPS请参考李洋个人博客
每一天,每一秒,你所做的决定都会改变你的人生!

发表评论

快捷回复:

评论列表 (暂无评论,79人围观)参与讨论

还没有评论,来说两句吧...

Top