当前位置: 首页 > news >正文

谷歌web第三方登录

1.在谷歌控制台创建客户端信息

https://console.cloud.google.com/auth/clients

注:在重定向的url中一定要是https开头的。

创建完成之后主要获取三个信息

clientID、secret、redirctUrl

 2.配置pom

<dependency><groupId>com.google.auth</groupId><artifactId>google-auth-library-oauth2-http</artifactId><version>0.22.2</version>
</dependency>
<dependency><groupId>com.google.api-client</groupId><artifactId>google-api-client</artifactId><version>1.32.1</version>
</dependency>

3.获取跳转授权的url

clientID:谷歌控制台创建的

public static String getLoginUrl(){String scope = "openid email profile";String redirectUri = "https://www.baidu.com";String authUrl = "https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=%s&redirect_uri=%s&scope=%s";return String.format(authUrl, clientId, redirectUri, scope);}

4.接收code,获取accessToken

这个地方需要如果只是自己测试可以直接将重定向的地址,配置成获取code的地址,如果是正式环境我的方案是配置前端的页面,然后有前端将code传给后端。

注意:由于是跳转,code会被转义,自己测试的时候注意decode。

private static String tokenEndpoint = "https://oauth2.googleapis.com/token";
public static String checkCode(String code){MultiValueMap<String, String> params = new LinkedMultiValueMap<>();params.add("client_id", clientId);params.add("client_secret", clientSecret);params.add("code", code);params.add("redirect_uri", "https://www.baidu.com");params.add("grant_type", "authorization_code");HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);RestTemplate restTemplate = new RestTemplate();ResponseEntity<Map> response = restTemplate.postForEntity(tokenEndpoint, request, Map.class);String accessToken = (String) response.getBody().get("access_token");return accessToken;}

5.根据accessToken获取用户信息

private static String userInfoEndpoint = "https://www.googleapis.com/oauth2/v3/userinfo";
public static Map getUserInfo(String accessToken){HttpHeaders userInfoHeaders = new HttpHeaders();userInfoHeaders.setBearerAuth(accessToken);HttpEntity<String> userInfoRequest = new HttpEntity<>(userInfoHeaders);RestTemplate restTemplate = new RestTemplate();ResponseEntity<Map> userInfoResponse = restTemplate.exchange(userInfoEndpoint, HttpMethod.GET, userInfoRequest, Map.class);Map<String, Object> userInfo = userInfoResponse.getBody();String googleId = (String) userInfo.get("sub");String email = (String) userInfo.get("email");String name = (String) userInfo.get("name");String picture = (String) userInfo.get("picture");return userInfo;}

相关文章:

  • .NET NativeAOT 指南
  • 鸿蒙OSUniApp打造多功能图表展示组件 #三方框架 #Uniapp
  • Java 重试机制详解
  • 鸿蒙OSUniApp 实现的二维码扫描与生成组件#三方框架 #Uniapp
  • 1688 平台 API 接口深度解析:高效获取商品详情数据的开发实践
  • 国产免费工作流引擎star 6.5k,Warm-Flow升级1.7.2(新增案例和修复缺陷)
  • LLaMA-Factory 微调 Qwen2-7B-Instruct
  • 【vim】--- vim 插件说明 超详细持续更新中
  • 车载网关--- 职责边界划分与功能解耦设计
  • JVM 精华
  • ChatGPT 能“记住上文”的原因
  • Awesome WM自定义菜单实现nas共享目录挂载
  • ClickHouse详解
  • ElasticSearch重启之后shard未分配问题的解决
  • Rocky Linux 9.5 基于kubeadm部署k8s
  • 微信小程序智能商城系统(uniapp+Springboot后端+vue管理端)
  • 安全扫描之 Linux 杀毒软件 Clamav 安装
  • 查询电脑伪装IP,网络安全速查攻略!
  • 代码随想录 算法训练 Day1:数组
  • 深度解析网闸策略:构建坚固的网络安全防线
  • 六连板成飞集成:航空零部件业务收入占比为1.74%,市场环境没有重大调整
  • 陕西省安康市汉阴县县长陈永乐已任汉阴县委书记
  • 一海南救护车在西藏无任务拉警笛开道,墨脱警方:已处罚教育
  • 周启鸣加盟同济大学,曾任香港浸会大学深圳研究院院长
  • 泽连斯基批准美乌矿产协议
  • 在对国宝的探索中,让美育浸润小学校园与家庭