跳转到内容

Android 上的 Firebase Google 登录

本指南将帮助你在 Android 上集成带有 Firebase Authentication 的 Google 登录。我假设你已经完成了通用 Firebase Google 设置

  1. console.cloud.google.com 转到项目概览

    Firebase Project Overview
  2. 点击 Add app 按钮

    Firebase Add App Button Firebase Add App Button
  3. 选择 Android

    Firebase Add App Android Button
  4. 填写表单的第一部分

    1. 填写 Android package name
      1. 使用 npx cap open android 在 Android Studio 中打开你的应用
      2. 在导航器的最底部,找到 Gradle Scripts Gradle Scripts section in Android Studio project navigator
      3. 找到模块 appbuild.gradle build.gradle (Module: app) file in Gradle Scripts section
      4. 复制 android.defaultConfig.applicationId。这将是 Firebase 控制台中的 package name Build.gradle file showing applicationId configuration
      1. 在 Firebase 控制台中粘贴 Firebase Add App Android Package Name Field
    2. 点击 Register app 按钮 Firebase Add App Android Register Button
  5. 跳过 Download and then add config file 步骤

    Firebase Add App Android Skip 'Download and then add config file'
  6. 跳过 Add firebase SDK 步骤

    Firebase Add App Android Skip 'Add firebase SDK'
  7. 点击 Continue to console 按钮

    Firebase Add App Android Continue to Console Button
  8. 如果未自动进行身份验证,请转到 settings -> general -> your apps -> 找到你的 Android 应用并点击它

    Firebase Add App Android Settings General Your Apps Button
  9. 获取你的 SHA1 指纹

    按照 Google Login Android 设置指南中的步骤 10-11:

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

      Terminal showing gradlew signInReport command
    2. 滚动到此命令的顶部。你应该看到以下内容。复制 SHA1

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

    1. 点击 Add fingerprint 按钮 Firebase Add App Android Add Fingerprint Button
    2. 粘贴你在上一步中复制的 SHA1 Firebase Add App Android Add Fingerprint SHA1 Field
    3. 点击 Save 按钮 Firebase Add App Android Add Fingerprint Save Button
  11. 获取你的 Web 客户端 ID

    1. 转到 Build -> Authentication Firebase Authentication Menu
    2. 点击 Sign-in method 按钮 Firebase Authentication Sign-in Method Button
    3. 点击 Google 提供商 Firebase Authentication Sign-in Method Google Provider
    4. 点击 Web SDK configuration 按钮 Firebase Authentication Sign-in Method Web SDK Configuration Button
    5. 复制 Web client ID。这将是插件的 initialize 方法中的 webClientId
    Firebase Authentication Sign-in Method Web SDK Configuration Web Client ID
  12. 在 JS 中使用 Web 客户端 ID。

此时,你技术上已准备好在 Android 上使用带有 Firebase Authentication 的 Google 登录。但是,我建议按照下一步中的说明在 Google Cloud 控制台中仔细检查设置。

在 Google Cloud 控制台中仔细检查设置

Section titled “在 Google Cloud 控制台中仔细检查设置”

为了确保设置正确,你应该在 Google Cloud 控制台中仔细检查设置。

  1. 转到 console.cloud.google.com

  2. 找到你的项目

    1. 点击项目选择器 Google Cloud Console Project Selector
    2. 按 Firebase 项目的确切名称搜索你的项目并点击它。在我的情况下,它是 sociallogin-tutorial-appFirebase Project Selector Project
  3. 打开搜索栏并打开 credentials

    1. 打开搜索栏 Google Cloud Console Search Bar
    2. 搜索 credentials 并点击 APIs and Services 那个(截图上的第 2 个) Google Cloud Console Credentials Search
  4. 验证你在列表中看到 Android 和 Web 客户端 ID。

    Google Cloud Console Credentials List
  5. 验证 Android 客户端 ID 在 Firebase 控制台中是否正确配置。

    1. 点击 Android 应用 Google Cloud Console Android App Select
    2. 确认 SHA1 哈希配置正确,并且与你在之前步骤中复制的匹配。 Google Cloud Console Android App SHA1 Configured
  6. 验证 Web 客户端 ID 在 Firebase 控制台中是否正确配置。

    1. 点击 Web 应用 Google Cloud Console Web App Select
    2. 确认客户端 ID 与你在之前步骤中复制的匹配。 Google Cloud Console Web App Client ID Configured

万岁!你现在已准备好在 Android 上使用带有 Firebase Authentication 的 Google 登录。

如果身份验证挂起或失败:

  • 验证 idToken audience 是否与你的 Firebase Web 客户端 ID 匹配
  • 检查 Firebase Console 中是否启用了 Google 登录
  • 确保 SHA-1 指纹配置正确
  • 查看示例应用代码以供参考