跳过内容

Google 登录设置

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 帐户

在本部分中,您将设置 Google 显示的登录屏幕。

  1. 请前往 console.cloud.google.com
  2. 点击项目选择器 Google Console 项目选择器
  3. 如果您尚未创建项目,请 创建新项目.
    1. 点击 New project Google Console 中的新项目按钮
    2. 输入项目名称并点击 Create 项目名称输入框和创建按钮
    3. 确保您在正确的项目中 项目名称显示在选择器中,表示正确的项目选择
  4. 开始配置Capgo OAuth consent screen
    1. 点击搜索栏

      Google Console 搜索栏
    2. 搜索 OAuth consent screen 并点击它

      显示 OAuth 同意屏幕选项的搜索结果
    3. 配置同意屏幕

      点击 create

      OAuth 同意屏幕用户类型选择(外部和内部选项)
  5. 填写关于您的应用的信息
    1. 让我们从 App Information

      显示应用名称和用户支持邮箱字段的应用信息部分开始
      • 请输入您的 App Name
      • 输入 user support email
      1. 可以 添加应用程序Logo。

        OAuth 许可屏幕中的应用程序 Logo 上传部分
      2. 应该 配置 App domain

        应用程序域名配置部分,授权域名字段
      3. 必须 提供开发人员的电子邮件

        开发人员联系信息部分,包含电子邮件字段
      4. 点击 save and continue

        保存并继续按钮,位于表单底部
  6. 配置权限范围
    1. 点击 add or remove scopes

      添加或删除权限按钮在权限配置屏幕中
    2. 选择以下权限并点击 update

      包含电子邮件和个人资料权限的权限选择对话框
    3. 点击 save and continue

      权限屏幕中的保存并继续按钮
  7. 添加测试用户
    1. 点击 add users 测试用户部分中的添加用户按钮
    2. 输入您的Google电子邮件,按回车键,然后点击 add 测试用户部分中的电子邮件输入字段和添加按钮
    3. 点击 save and continue 测试用户屏幕中的保存并继续按钮
  8. 点击 back to dashboard 完成页面底部的返回到仪表板按钮
  9. 提交应用程序进行验证

在线访问与离线访问的区别

标题:在线访问与离线访问的区别

使用Capacitor的Google登录有多种方式。以下是两种方式的区别总结表格:

在线访问离线访问
需要后端
长期访问令牌
易于设置

在这种情况下,请选择在线访问。

  1. 您的应用程序将在客户端调用一些Google API,但永远不会从后端调用

    在这种情况下,请选择在线访问。

  2. Your app will call some Google APIs from the client, but never from the backend

    In this case, choose online access.

  3. 您的应用程序将在后端调用一些谷歌 API,但只有当用户正在积极使用应用程序时才会调用

    在这种情况下,请选择在线访问

  4. 您的应用程序将定期检查用户的日历,即使用户不在使用应用程序

    在这种情况下,请选择离线访问

在线访问的示例后端

标题:在线访问的示例后端

在本教程的这一部分,我将展示如何在您的后端验证用户。

该示例将非常简单,并且将基于以下技术:

您可以在此示例中找到 code 这里

如您所见:

VS Code 展示 Google 认证 code 验证令牌

这个想法很简单。您发送一个简单的请求到 GET 并且这个返回您令牌是否有效或无效,并且如果有效,它会给您用户的电子邮件。它也会给您一些关于用户令牌的其他信息。 https://www.googleapis.com/oauth2/v3/tokeninfo Google OAuth Playground 展示令牌信息响应,包含用户详细信息

从那里,您可以向用户发出自己的 JWT 或发出某种类型的会话 cookie。可能性无穷尽,用于最终的认证实现。

如果您想调用 Google __CAPGO_KEEP_0__,我强烈建议您查看

If you do want to call Google API’s, I would strongly recommend looking at 。从那里您可以轻松看到您可以调用的 API。使用离线访问与您的后端

使用离线访问与您的后端

使用本地访问与您的后端

为了使用离线访问,您需要以下内容:

  • HTTP 服务器

在本例中,我将使用以下技术来为我的应用程序提供离线访问:

本例的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());
}
}

注意这里缺少了什么:应用中没有 SocialLogin.refresh() call。这个是有意为之的。在 Google 离线模式下,刷新发生在您的后端与 serverAuthCode 交换并安全存储刷新令牌之后。

如果您正在使用 Google 登录设置 来规划身份验证和帐户流程, Using @capgo/capacitor-social-login 使用 @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 的实现细节, 和 双因素认证 为双因素认证的实现细节