跳过内容

Firebase Google Login on Android

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. At the very bottom of the navigator, find the Gradle Scripts Android Studio 项目导航器中的 Gradle Scripts 部分
      3. Find build.gradle 为该模块 app build.gradle (Module: app) 文件在 Gradle Scripts 部分
      4. Copy the android.defaultConfig.applicationId。 这将是您的 package name 在 Firebase 控制台中 Build.gradle 文件显示 applicationId 配置
      1. 在 Firebase 控制台中粘贴它 Firebase 添加 App Android 包名字段
    2. 点击 Register app 按钮 Firebase 添加 App Android 注册按钮
  5. 跳过 Download and then add config file 步骤

    Firebase 添加 App Android 跳过 '下载并添加配置文件'
  6. 跳过 Add firebase SDK 步骤

    Firebase 添加 App Android 跳过 '添加 firebase SDK'
  7. 点击 Continue to console 按钮

    Firebase 添加 App Android 继续到控制台按钮
  8. 如果您没有自动登录,请前往 settings -> general -> your apps -> 在 Android 应用程序中找到并点击它

    Firebase 添加 App Android 设置 通用应用程序按钮
  9. 获取您的 SHA1 指纹

    按照第 10-11 步从 Google 登录 Android 设置指南:

    1. 现在,打开终端。确保您在应用程序的文件夹中并运行 android 终端显示 gradlew signInReport 命令 ./gradlew signInReport

      确保您在应用程序的文件夹中并运行
    2. Scroll to the top of this command. You should see the following. Copy the SHA1.

      Terminal output showing SHA1 certificate fingerprint
  10. 将SHA1添加到Firebase项目中

    1. 点击 Add fingerprint button Firebase 添加 App Android 添加 FingerPrint 按钮
    2. 粘贴您在上一步中复制的 SHA1 值 Firebase 添加 App Android 添加 FingerPrint SHA1 字段
    3. 点击 Save button Firebase 添加 App Android 添加 FingerPrint 保存按钮
  11. 获取您的 Web 客户端 ID

    1. 前往 Build -> Authentication Firebase 认证菜单
    2. 点击 Sign-in method button Firebase Authentication Sign-in Method Button
    3. 点击 Google 服务提供商 Firebase Authentication Sign-in Method Google 服务提供商
    4. 点击 Web SDK configuration 按钮 Firebase Authentication Sign-in Method Web SDK 配置按钮
    5. 复制 Web client ID. 这将是你的 webClientIdinitialize 插件的
    Firebase Authentication Sign-in Method Web SDK Configuration Web Client ID
  12. 使用 Web 客户端 ID 在 JS 中。

在这个阶段,您 技术上 准备好使用 Google Sign-In 与 Firebase Authentication 在 Android 上。然而,我建议双重检查 Google Cloud 控制台中的设置,如下一步中所解释的那样。

双重检查 Google Cloud 控制台中的设置

标题为“双重检查 Google Cloud 控制台中的设置”

为了确保设置正确,请在 Google Cloud 控制台中重新检查设置。

  1. 前往 console.cloud.google.com

  2. 找到您的项目

    1. 点击项目选择器 Google Cloud 控制台项目选择器
    2. 通过您的 Firebase 项目的准确名称搜索您的项目并点击它。在我的情况下,它是 sociallogin-tutorial-app. Firebase 项目选择器项目
  3. 打开搜索栏并打开 credentials

    1. 打开搜索栏 Google Cloud 控制台搜索栏
    2. 搜索 credentials 点击并选择 APIs and Services 截图中的第二个(第 2 个) Google 云端控制台凭据搜索
  4. 确认您在列表中看到 Android 和 Web 客户端 ID

    Google 云端控制台凭据列表
  5. 确认 Android 客户端 ID 在 Firebase 控制台中正确配置

    1. 点击 Android 应用 Google Cloud Console Android App 选择
    2. __CAPGO_KEEP_0__ Google Cloud Console Android App SHA1 配置
  6. 请在 Firebase 控制台中确认 Web 客户端 ID 是否正确配置

    1. 点击 Web 应用 Google Cloud Console Web App 选择
    2. __CAPGO_KEEP_0__ Google Cloud Console Web App 客户端 ID 配置

Voila! 您现在已经准备好使用 Google Sign-In 与 Firebase Authentication 在 Android 上。

如果认证卡住或失败:

  • 验证 idToken 您的 Firebase web 客户端 ID
  • 检查 Firebase 控制台中是否启用了 Google Sign-In
  • 确保 SHA-1 指纹正确配置
  • 查看 示例应用 code 以参考

继续从 Firebase Google Login on Android

继续从 Firebase Google Login on Android

如果您正在使用 Firebase Google Login on Android 来规划身份验证和帐户流程,连接它与 使用 @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 的实现细节 双因素认证 对于双因素认证的实现细节