# Google

# 实现 Google 第三方授权登录

目前Google的第三方登录有很多方案,且官方提供SDK方便接入。但是我这个项目同时要实现网页和客户端。所以选择了 Google OAuth 2.0 (opens new window) 的方案。

img

# 配置 Google Cloud Platform

# 首先访问 Google Cloud Platform 控制台

地址: https://console.cloud.google.com/projectselector2/home?pli=1 (opens new window)

img

# OAuth 同意屏幕

在左边的侧边栏中选择 API和服务

img

WARNING

测试账户只能添加不能删除且限制100个

img

# 网域验证

img

img

# 凭据

TIP

客户端ID用户用户登录,密钥用于服务器验证Token是否有效以及获取用户信息

img

WARNING

授权javascript来源和重定向来源要确保域名地址的完整比如www是否有无(且只支持https协议连接)

# 前端获取用户tokenId

以react为例 通过react-google-login来实现获取用户tokenId

npm install react-google-login

import GoogleLogin from 'react-google-login';

<GoogleLogin
clientId="658977310***55225" //clientId 为上图客户端秘钥
render={renderProps => (
    <button onClick={renderProps.onClick} disabled={renderProps.disabled}>This is my custom Google button</button>
)}
buttonText="Login"
onSuccess={responseGoogle}
onFailure={responseGoogle}
cookiePolicy={'single_host_origin'}
/>

TIP

通过responseGoogle回调函数可以获得用户的信息,然后获取相对应的tokenId给后端

最后更新时间: 2/28/2023, 8:33:37 PM