SpringBoot实现三方登录之--------微博登录

SpringBoot实现三方登录之--------微博登录

登录接入流程:

使用步骤:

  1. 一个静态页面写一个链接:点击微博登录进入微博授权.
    在这里插入图片描述
    2.输入账号密码,点击登录,会自动授权,授权成功就会展示当前账户的一些信息.
    在这里插入图片描述
    3.显示出来的个人信息: 例如微博名字,描述等. 到时候你要啥 取啥即可!
    在这里插入图片描述

下面来说一下具体实现步骤:

  • 准备工作: 进入微博的开放平台去注册应用先. 开放平台地址: https://open.weibo.com/
	进入以后点击: 网站接入  

在这里插入图片描述

- 进入之后填写自己的信息.  一些信息可以根据自己的业务来.  我的只是个Demo.  信息瞎填的.别纠结这些.

在这里插入图片描述

填好以后进行邮箱验证  ,验证好之后,填写高级信息   是内网穿透的地址; 示图如下:

在这里插入图片描述

那么就需要内网穿透工具:    下载地址:  https://www.cpolar.com/   注册一个,安装,根据网站提示使用 
  • 根据提示操作之后: 高级信息将地址填入: 两个地址哪个都可以

在这里插入图片描述

开始代码编辑:

需要用到的三个连接:

:
https://api.weibo.com/oauth2/authorize?client_id=你的appkey&response_type=code&redirect_uri=你的回调地址 (微博请求你的时候会在你的回调地址后加一个code参数)
主要的两个参数:
client_id 这个就是创建应用的App Key
redirect_uri 这个就是刚刚编辑的授权回调函数

二:
https://api.weibo.com/oauth2/access_token (这个网址是要根据你上面的code来获取用户的access_token和用户的uid)  注意: 需要用POST请求
参数使用Map 封装起来
		  String uri="https://api.weibo.com/oauth2/access_token";
        Map<String,Object> map=new HashMap<>();
        map.put("client_id",你的App Key);
        map.put("client_secret","你的secret");
        map.put("grant_type","authorization_code");
        map.put("redirect_uri","回调地址");
        map.put("code",回调的参数值code);
        String post = HttpUtil.post(uri, map);
参数具体可以参照wiki: https://open.weibo.com/wiki/Oauth2/access_token

三:
用Json 转成 Map 对象
  Map<String,String> parse = (Map<String, String>) JSON.parse(post);
        String access_token=parse.get("access_token");
        String uid=parse.get("uid");
https://api.weibo.com/2/users/show.json?uid=" + uid +&access_token=+ accessToken(uid是从上面取出来的,access_token也是从上面取出的)  但是这是一个GET请求
这个网址的返回结果就是用户的各种信息

测试代码

HTML
<!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=800800070&response_type=code&redirect_uri=http://49e50aac.cpolar.io/code">微博登录</a>
  </body>
</html>
使用的Hutool工具类,在pom文件导入依赖即可调用.
<dependency>
  <groupId>cn.hutool</groupId>
  <artifactId>hutool-all</artifactId>
  <version>5.3.5</version>
</dependency>
Controller
package com.weibo.weibo.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class weiboController {
  @RequestMapping("/code")
  public String weibo(@RequestParam("code") String code){
    String uri="https://api.weibo.com/oauth2/access_token";
    Map<String,Object> map=new HashMap<>();
    map.put("client_id",800800070);
    map.put("client_secret","d2c4f0c53f0108833892c0465758404f");
    map.put("grant_type","authorization_code");
    map.put("redirect_uri","http://49e50aac.cpolar.io/code");
    map.put("code",code);
    String post = HttpUtil.post(uri, map);
    Map<String,String> parse = (Map<String, String>) JSON.parse(post);
    String access_token=parse.get("access_token");
    String uid=parse.get("uid");
    String s = HttpUtil.get("https://api.weibo.com/2/users/show.json?access_token=" + access_token + "&uid=" + uid);
    return s;
  }
}

有不对的大家可以提出来,指点指点,感谢大家!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值