跳转到正文

Android 上的 Firebase Google Login

GitHub

本指南将帮你在 Android 上集成 Google Sign-In 与 Firebase Authentication。这里假设你已经完成了 Firebase Google 通用配置

  1. 前往您的项目概览页面 console.cloud.google.com

    Firebase 项目概览
  2. 点击 Add app 按钮

    Firebase 添加应用按钮 Firebase 添加应用按钮
  3. 选择 Android

    Firebase 添加应用 Android 按钮
  4. 填写表单的第一部分

    1. 填写 Android package name
      1. 使用以下命令在 Android Studio 中打开您的应用 npx cap open android
      2. 在导航器最底部,找到 Gradle Scripts Android Studio 项目导航器中的 Gradle Scripts 部分
      3. 找到 build.gradle 该模块的 app Gradle Scripts 部分中的 build.gradle (Module: app) 文件
      4. 复制 android.defaultConfig.applicationId。这将是你的 package name 在 Firebase 控制台中 显示 applicationId 配置的 Build.gradle 文件
      1. 将其粘贴到 Firebase 控制台中 Firebase 添加应用 Android 包名输入框
    2. 点击 Register app 按钮 Firebase 添加应用 Android 注册按钮
  5. 跳过此 Download and then add config file 步骤

    Firebase 添加 Android 应用:跳过“下载并添加配置文件”
  6. 跳过此 Add firebase SDK 步骤

    Firebase 添加 Android 应用:跳过“添加 firebase SDK”
  7. 点击 Continue to console 按钮

    Firebase 添加 Android 应用:点击“继续前往控制台”按钮
  8. 如果未自动完成身份验证,请前往 settings -> general -> your apps -> 找到你的 Android 应用并点击它

    Firebase 添加 Android 应用:设置 > 常规 > “你的应用”按钮
  9. 获取您的 SHA1 指纹

    请按照 Google 登录 Android 配置指南:

    1. 现在,打开终端。确保你位于 android 你的应用文件夹并运行 ./gradlew signInReport

      终端显示 gradlew signInReport 命令
    2. 滚动到这条命令的顶部。你应该会看到以下内容。复制 SHA1.

      显示 SHA1 证书指纹的终端输出
  10. 将 SHA1 添加到 Firebase 项目

    1. 点击 Add fingerprint 按钮 Firebase 添加应用 Android 添加指纹按钮
    2. 粘贴上一步复制的 SHA1 Firebase 添加应用 Android 添加指纹 SHA1 字段
    3. 点击 Save 按钮 Firebase 添加 Android 应用:添加指纹、保存按钮
  11. 获取你的 Web Client ID

    1. 前往 Build -> Authentication Firebase Authentication 菜单
    2. 点击 Sign-in method 按钮 Firebase Authentication 登录方法按钮
    3. 点击 Google 提供方 Firebase Authentication 登录方法 Google 提供方
    4. 点击 Web SDK configuration 按钮 Firebase Authentication 登录方式 Web SDK 配置按钮
    5. 复制 Web client ID。这将是你的 webClientId ,详见 initialize 插件方法。
    Firebase Authentication 登录方式 Web SDK 配置 Web Client ID
  12. 在 JS 中使用 web client ID。

至此,你已 在技术上 可以在 Android 上使用 Firebase Authentication 配合 Google Sign-In。不过,建议按照下一步的说明,再次检查 Google Cloud 控制台的配置。

为确保配置正确,你应该再次检查 Google Cloud 控制台的设置。

  1. 前往 console.cloud.google.com

  2. 找到你的项目

    1. 点击项目选择器 Google Cloud Console 项目选择器
    2. 通过 Firebase 项目的准确名称搜索您的项目,然后点击它。在我的例子中,它是 sociallogin-tutorial-app. Firebase 项目选择器项目
  3. 打开搜索栏并打开 credentials

    1. 打开搜索栏 Google Cloud Console 搜索栏
    2. 搜索 credentials 并点击 APIs and Services 一个(截图中的编号 2) Google Cloud Console 凭据搜索
  4. 确认您在列表中同时看到 Android 和 Web 客户端 ID。

    Google Cloud Console 凭据列表
  5. 请确认 Firebase 控制台中已正确配置 Android 客户端 ID。

    1. 点击 Android 应用 Google Cloud Console 选择 Android App
    2. 确认 SHA1 哈希值配置正确,且与你在之前步骤中复制的内容一致。 Google Cloud Console Android App SHA1 已配置
  6. 请确认 Firebase 控制台中已正确配置 Web 客户端 ID。

    1. 点击 Web 应用 选择 Google Cloud Console Web App
    2. 请确认 Client ID 与你在前几步中复制的一致。 Google Cloud Console Web App Client ID 已配置完成

大功告成!你现在可以在 Android 上通过 Firebase Authentication 使用 Google Sign-In 了。

如果身份验证卡住或失败:

  • 请检查 idToken audience 与你的 Firebase Web 客户端 ID 匹配
  • 检查 Firebase 控制台中是否已启用 Google 登录
  • 确保 SHA-1 指纹已正确配置
  • 请查阅 示例应用 code 仅供参考

继续配置 Android 上的 Firebase Google 登录

标题为“继续配置 Android 上的 Firebase Google 登录”的章节

如果你正在使用 Android 上的 Firebase Google Login 用来规划身份验证和账户流程,并将它与 使用 @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 中的实现细节, 双因素认证 有关双重身份验证的实现细节。