@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";
}
}
# 应用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=你的回调地址
<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登录(第三方登录)帮朋友推荐》
还没有评论,来说两句吧...