Google登录设置
复制一个包含安装步骤和本插件的完整 Markdown 指南的配置提示。
简介
简介In this guide, you will learn how to setup Google Login with Capgo Social Login. You will need the following in order to setup Google Login:
- Google 帐户
基本设置
注意请访问
- console.cloud.google.com __CAPGO_KEEP_0__
- 点击项目选择器
- 如果您还没有项目,请 创建新项目.
- 点击
New project
- 为项目命名并点击
Create
- 确保您在正确的项目中
- 点击
- 开始配置
OAuth consent screen-
点击搜索栏
-
搜索并点击
OAuth consent screen并点击
-
配置同意屏幕
点击
create
-
- 填写您的应用信息
-
让我们从
App Information
- 请输入您的
App Name - 输入
user support email
-
的信息 CAN 添加应用程序Logo。
-
您 应该 配置
App domain
-
您 必须 提供开发者的电子邮件
-
点击
save and continue
- 请输入您的
-
- 配置权限范围
-
点击
add or remove scopes
-
选择以下范围并点击
update
-
点击
save and continue
-
- 添加测试用户
- 点击
add users
- 输入您的Google邮箱,点击回车,然后点击
add
- 点击
save and continue
- 点击
- 点击
back to dashboard
- 提交您的应用程序进行验证
在线访问与离线访问的区别
标题为“在线访问与离线访问的区别”使用Capacitor的Google登录有多种方式。以下是两种方式的区别表格:
| 在线访问 | 离线访问 | |
|---|---|---|
| 需要后端 | ❌ | ✅ |
| 长期访问令牌 | ❌ | ✅ |
| 易于设置 | ✅ | ❌ |
替换为访问和刷新令牌,然后在后端刷新这些令牌。
-
如果您仍然不知道应该选择哪一个,请考虑以下场景:
您希望用户登录,立即之后您将为他发行一个自定义 JWT。您的应用程序将不会调用 Google API
-
在这种情况下,请选择在线访问。
您的应用程序将在客户端调用一些 Google API,但永远不会从后端调用
-
在这种情况下,请选择在线访问
在这种情况下,选择在线访问
-
您的应用程序将定期检查用户的日历,即使用户不在主动使用应用程序时
在这种情况下,选择离线访问
在线访问的示例后端
标题:在线访问的示例后端在本教程的这一部分,我将展示如何在您的后端验证用户。
这个例子将非常简单,它将基于以下技术:
您可以在这里找到 code 的示例 这里
您可以看到:
这个想法很简单。您发送一个简单的请求到 GET 并且这个返回您令牌是否有效或无效,以及如果有效,则给您用户的电子邮件。它还给您一些关于用户令牌的其他信息 https://www.googleapis.com/oauth2/v3/tokeninfo Google OAuth Playground 展示令牌信息响应,包含用户详细信息
如果您想调用 Google __CAPGO_KEEP_0__,我强烈建议您查看
If you do want to call Google API’s, I would strongly recommend looking at 。从那里您可以轻松看到您可以调用的 API使用离线访问与您的后端
标题:使用离线访问与您的后端
If you do want to call Google __CAPGO_KEEP_0__’s, I would strongly recommend looking at为了使用离线访问功能,您需要以下内容:
- 一个HTTP服务器
在本例中,我将使用以下技术来为我的应用提供离线访问功能:
-
LowDb (一个简单的数据库)
本例的code可以在 这里
客户端code的外观如下:
import { Capacitor } from '@capacitor/core';import { GoogleLoginOfflineResponse, SocialLogin } from '@capgo/capacitor-social-login';import { usePopoutStore } from '@/popoutStore'; // <-- specific to my app
const baseURL = "[redacted]";
async function fullLogin() { await SocialLogin.initialize({ google: { webClientId: '[redacted]', iOSClientId: '[redacted]', iOSServerClientId: 'The same value as webClientId', mode: 'offline' // <-- important } }) const response = await SocialLogin.login({ provider: 'google', options: { forceRefreshToken: true // <-- important } })
if (response.provider === 'google') { const result = response.result as GoogleLoginOfflineResponse const res = await fetch(`${baseURL}/auth/google_offline`, { headers: { "Content-Type": "application/json" }, body: JSON.stringify({ serverAuthCode: result.serverAuthCode, platform: Capacitor.getPlatform() }), method: "POST" })
if (res.status !== 200) { popoutStore.popout("Full google login failed", "check console"); return }
const { jwt } = await res.json(); const userinfo = await fetch(`${baseURL}/auth/get_google_user`, { headers: { Authorization: `Bearer ${jwt}` } }) if (userinfo.status !== 200) { popoutStore.popout("Full google (userinfo) login failed", "check console"); return } popoutStore.popout("userinfo res", await userinfo.text()); }}注意这里缺少了什么:没有在应用中调用。那样是有意为之的。在 Google 离线模式下,刷新发生在您的后端与 Google 交换并安全存储刷新令牌之后。 SocialLogin.refresh() 继续从 Google 登录设置 serverAuthCode 如果您正在使用
Google 登录设置
来规划身份验证和帐户流程,连接它到使用 @__CAPGO_KEEP_0__/__CAPGO_KEEP_1__-social-login 为原生能力在使用 @__CAPGO_KEEP_0__/__CAPGO_KEEP_1__-social-login 中 使用 @__CAPGO_KEEP_0__/__CAPGO_KEEP_1__-social-login Using @capgo/capacitor-social-login for the native capability in Using @capgo/capacitor-social-login, @capgo/capacitor-social-login 在 @capgo/capacitor-social-login 中的实现细节 @capgo/capacitor-passkey 在 @capgo/capacitor-passkey 中的实现细节 @capgo/capacitor-native-biometric 在 @capgo/capacitor-native-biometric 中的实现细节, 双因素认证 在双因素认证中的实现细节。