注册微博开放平台
注册地址: https://open.weibo.com/
之后选择网站接入填写对应信息
基本信息照的这个填写:
高级信息是你内网穿透的地址
内网穿透
内网穿透工具地址:https://www.cpolar.com/
下载 跑起来之后是这个样子
有一个http的请求和一个https的请求,用哪个都是一样的,这样就可以被外网访问到了
https://api.weibo.com/oauth2/authorize?client_id=你的appkey&response_type=code&redirect_uri=你的回调地址 (微博请求你的时候会在你的回调地址后加一个code参数,这个参数你需要获取一下) 主要两个参数:
https://api.weibo.com/oauth2/access_token (这个网址是要根据你上面的code来获取用户的access_token和用户的uid) 这是一个post请求 !!!!!! 主要的参数 Map<String,Object> parmMap = new HashMap<>(); parmMap.put("client_id",你的App Key); parmMap.put("client_secret","你的secret"); parmMap.put("grant_type","authorization_code"); parmMap.put("redirect_uri","http://4b81f251.cpolar.io/wbLogin"); //回调地址 parmMap.put("code",code); //这个code是刚刚回调请求的参数值 这些参数具体可以参照wiki: https://open.weibo.com/wiki/Oauth2/access_token
https://api.weibo.com/2/users/show.json?uid=" + uid + “&access_token=” + accessToken(uid是从上面的里面取出来的,access_token也是从上一个地址取出的) 这是一个get请求 最后这个网址的返回结果就是用户的各种信息,之后就自己操作了
发送http请求选择使用了hutool
hutool官网的地址 https://www.hutool.cn/
pom依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.5</version>
</dependency>
编写controller的代码
package com.wbloginthree.demo.controller;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
/**
* @author 小哥
*/
@Controller
public class LoginHandler {
@RequestMapping("/wbLogin")
@ResponseBody
public String wbLogin(String code){
Map<String,Object> parmMap = new HashMap<>();
parmMap.put("client_id",你的appkey);
parmMap.put("client_secret","你的secret");
parmMap.put("grant_type","authorization_code");
parmMap.put("redirect_uri","http://4b81f251.cpolar.io/wbLogin");
parmMap.put("code",code);
String post = HttpUtil.post("https://api.weibo.com/oauth2/access_token", parmMap);
Map<String,String> parse = (Map<String, String>) JSON.parse(post);
String accessToken = parse.get("access_token");
System.out.println(accessToken);
String uid = parse.get("uid");
String s = HttpUtil.get("https://api.weibo.com/2/users/show.json?uid=" + uid + "&access_token=" + accessToken);
return s;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="https://api.weibo.com/oauth2/authorize?client_id=你的appkey&response_type=code&redirect_uri=你的回调地址">微博登录</a>
</body>
</html>