原创

第三方登录系列之微博登录篇

温馨提示:
本文最后更新于 2018年07月03日,已超过 2,368 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

第一步:申请微博·开放平台的开发者账号

第二步:创建网站应用

file

第三步:完善应用基本信息,然后提交审核

file
注意,本页面中的App KeyApp Secret会在后面代码中用到

第四步:完善OAuth2.0 授权设置

file

第五步:寻找对应的官方API接口

OAuth2接口开发文档。本例中共使用到三个相关api:

  1. 请求授权
  2. 获取授权Token
  3. 获取用户信息

第六步:编写相关代码

一般的第三方登录的流程基本如下:

  1. 访问授权地址,获取授权code
  2. 通过code换取相应token
  3. 使用token获取相关用户信息

6.1 获取授权code

在页面上添加一个a标签:

<a href="https://api.weibo.com/oauth2/authorize?client_id=[App Key]&response_type=code&redirect_uri=[授权回调页]">新浪微博登录</a>

当页面上点击该连接时,会自动跳转到授权页
file
在授权页成功登录后,就会回调到“第四步:完善OAuth2.0 授权设置”中配置的授权回调路径

6.2 通过code换取token(关键代码)

/**
 * 获取weibo access_token的地址
 */
String GET_WEIBO_ACCESS_TOKEN_URL = "https://api.weibo.com/oauth2/access_token";
/**
 * App Key
 */
String WEIBO_CLIENT_ID = "App Key";
/**
 * App Secret
 */
String WEIBO_CLIENT_SECRET = "App Secret";
/**
 * 此路径为OAuth2.0 授权设置配置的授权回调页地址
 */
String REDIRECT_URL = "授权回调页";

// step1: 通过code获取token
String url = GET_WEIBO_ACCESS_TOKEN_URL + "?client_id=" + WEIBO_CLIENT_ID + "&client_secret=" + WEIBO_CLIENT_SECRET + "&grant_type=authorization_code&code=" + code + "&redirect_uri=" + REDIRECT_URL;
System.out.println("获取token: " + url);
String accessTokenStr = RestClientUtil.post(url, null, null);
System.out.println("获取Token接口响应内容:" + accessTokenStr);
if (StringUtils.isEmpty(accessTokenStr)) {
    throw new RuntimeException("未获取到授权token");
}
JSONObject accessTokenObject = JSONObject.parseObject(accessTokenStr);
if (accessTokenObject.containsKey("error")) {
    throw new RuntimeException("获取授权token发生异常," + accessTokenStr);
}
// step2: 获取weibo用户信息
String accessToken = accessTokenObject.getString("access_token");
System.out.println("用户token:" + accessToken);
String uid = accessTokenObject.getString("uid");
System.out.println("用户ID:" + uid);

6.3 使用token获取相关用户信息(关键代码)

/**
 * 获取weibo用户信息的地址
 */
String GET_WEIBO_USER_INFO_URL = "https://api.weibo.com/2/users/show.json";

String getUserInfoUrl = GET_WEIBO_USER_INFO_URL + "?uid=" + uid + "&access_token=" + accessToken;
System.out.println("获取用户信息: " + getUserInfoUrl);
Map<String, String> requestHeader = new HashMap<>();
requestHeader.put("Authorization", "OAuth2 " + accessToken);
requestHeader.put("API-RemoteIP", InetAddress.getLocalHost().getHostAddress());

String userInfo = RestClientUtil.get(getUserInfoUrl, requestHeader);
System.out.println("获取到的userInfo:" + userInfo);

6.4 详细代码

/**
 * 获取weibo access_token的地址
 */
private static final String GET_WEIBO_ACCESS_TOKEN_URL = "https://api.weibo.com/oauth2/access_token";
/**
 * 获取weibo用户信息的地址
 */
private static final String GET_WEIBO_USER_INFO_URL = "https://api.weibo.com/2/users/show.json";
/**
 * App Key
 */
private static final String WEIBO_CLIENT_ID = "App Key";
/**
 * App Secret
 */
private static final String WEIBO_CLIENT_SECRET = "App Secret";
/**
 * 此路径为OAuth2.0 授权设置配置的授权回调页地址
 */
private static final String REDIRECT_URL = "授权回调页";

@GetMapping("/weibo")
public ModelAndView registeredByWeibo(String code) throws UnknownHostException {
    // step1: 通过code获取token
    String url = GET_WEIBO_ACCESS_TOKEN_URL + "?client_id=" + WEIBO_CLIENT_ID + "&client_secret=" + WEIBO_CLIENT_SECRET + "&grant_type=authorization_code&code=" + code + "&redirect_uri=" + REDIRECT_URL;
    System.out.println("获取token: " + url);
    String accessTokenStr = RestClientUtil.post(url, null, null);
    System.out.println("获取Token接口响应内容:" + accessTokenStr);
    if (StringUtils.isEmpty(accessTokenStr)) {
        throw new RuntimeException("未获取到授权token");
    }
    JSONObject accessTokenObject = JSONObject.parseObject(accessTokenStr);
    if (accessTokenObject.containsKey("error")) {
        throw new RuntimeException("获取授权token发生异常," + accessTokenStr);
    }
    // step2: 获取weibo用户信息
    String accessToken = accessTokenObject.getString("access_token");
    System.out.println("用户token:" + accessToken);
    String uid = accessTokenObject.getString("uid");
    System.out.println("用户ID:" + uid);
    String getUserInfoUrl = GET_WEIBO_USER_INFO_URL + "?uid=" + uid + "&access_token=" + accessToken;
    System.out.println("获取用户信息: " + getUserInfoUrl);
    Map<String, String> requestHeader = new HashMap<>();
    requestHeader.put("Authorization", "OAuth2 " + accessToken);
    requestHeader.put("API-RemoteIP", InetAddress.getLocalHost().getHostAddress());

    String userInfo = RestClientUtil.get(getUserInfoUrl, requestHeader);
    System.out.println("获取到的userInfo:" + userInfo);
    JSONObject object = JSONObject.parseObject(userInfo);
    System.out.println("用户首页:" + object.getString("url"));
    System.out.println("用户名:" + object.getString("name"));
    System.out.println("用户昵称:" + object.getString("screen_name"));
    System.out.println("用户头像:" + object.getString("profile_image_url"));
    System.out.println("用户地址:" + object.getString("location"));
    System.out.println("用户性别:" + object.getString("gender"));
    return ResultUtil.redirect("/");
}

第七步:调试代码

file

赶紧去试试吧...

正文到此结束
本文目录