写在最前面
微博登录的流程:
①开发者如何让用户选择微博登录后跳转到微博登录页??
https://api.weibo.com/oauth2/authorize?client_id=你的应用的APPKey&response_type=code&redirect_uri=你的授权回调地址
②用户登录了,我如何知道用户登录了???
微博需要你告诉微博,用户登录后,如何把用登录的信息告诉开发者,
这就需要开发者提供你个 途经,这个途经就是我 应用中的一个访问地址
也就是 回调地址
③微博访问 开发者的回调地址,会给回调地址一个 code
④开发者的提供的这个地址,需要接收这个code。
⑤开发者拿到这个code ,需要再去微博 获取一个 access_token,
⑥开发者拿到access_token,拿着这个token去获取用户信息即可
直接上代码:
springboot,restTemplate需要配一下,注入 ,
objectMapper 需要注入 ObjectMappper
@ResponseBody
@RequestMapping("weiboLogin")
public String weiBoCallBack(String code) throws IOException {
System.out.println(code);
String url="https://api.weibo.com/oauth2/access_token?client_id=423350222&client_secret=384bbcba00d5c28b423270ef252&grant_type=authorization_code&redirect_uri=http://www.gaoxi.com/weiboLogin&code="+code;
MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, map, String.class);
String body = stringResponseEntity.getBody();
Map map1 = null;
try {
map1 = objectMapper.readValue(body, Map.class);
} catch (IOException e) {
e.printStackTrace();
}
Object access_token = map1.get("access_token");
System.out.println(access_token);
Object uid = map1.get("uid");
String userInfoUrl="https://api.weibo.com/2/users/show.json?uid="+uid+"&access_token="+access_token;
String forObject = restTemplate.getForObject(userInfoUrl, String.class);
Map map2 = objectMapper.readValue(forObject, Map.class);
Object profile_image_url = map2.get("profile_image_url");
Object name = map2.get("name");
System.out.println(name+" "+profile_image_url);
return "hello";
}
1.首先申请开发者账号
①网址:https://open.weibo.com/
2.点击网址接入,立即接入
①第一次接入需要公司,个人信息,尽可能真实的填写
②创建应用时,需要填什么,就填啥,尽可能的真实(不知道的东西可以随便填)
③应用申请到开发这一步即可
3.打开我的应用,点击应用信息
基本信息
①应用地址,安全域名(www.gaoxi.com 是我hosts上配的)
127.0.0.1 www.gaoxi.com
高级信息这个非常的重要(*****)
① 授权回调页面是:你打开运行你的程序后,通过浏览器输入后能访问到的一个地址。比如我启动程序后,来到浏览器,输入 www.gaoxi.com/weiboLogin。是可以到达我后台的一个方法的。
②取消授权回调页,先不管
③应用服务ip地址 127.0.0.1
4.访问流程
①用户点击网页上的一个微博登录按钮
点击后,其实他连接到了一个地址,这个地址要这样写:
https://api.weibo.com/oauth2/authorize?client_id=你的应用的APPKey&response_type=code&redirect_uri=你的授权回调地址
对应:
和
所以我就在页面上设置个 链接到
https://api.weibo.com/oauth2/authorize?client_id=42335622&response_type=code&redirect_uri=http://www.gaoxi.com/weiboLogin (我这个应用对应的地址)
则会出现:(我们想看到的) 用自己开发者微博登录(别的好像不行)
②你拿着自己的账号登录后,会自动访问你的回调地址 ,就是刚才一直说的,
它访问的格式:www.gaoxi.com/weiboLogin?code=123212;
他会给你的code。
③通过code来获取access_token:
你需要通过post请求访问一个网址:
https://api.weibo.com/oauth2/access_token?client_id=你的appkey&client_secret=你的appsecret&grant_type=authorization_code&redirect_uri=http://www.gaoxi.com/weiboLogin&code=你获取的code;
对应我的应用,我应该通过post访问
https://api.weibo.com/oauth2/access_token?client_id=423356222&client_secret=3bbcb0d5c28b42f0d683270ef252&grant_type=authorization_code&redirect_uri=http://www.gaoxi.com/weiboLogin&code=123212";
我们可以手动通过postman测试:
④我们拿到了 access_token 和 uid ,我们可以访问用户个人信息了,要访问
“https://api.weibo.com/2/users/show.jsonuid=”+uid+"&access_token="+access_token
这个网址就可以访问到了(通过Get请求)