This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

使用 Google 身份验证

使用 react-native-nitro-google-signin 或 @react-native-google-signin/google-signin 在你的 Expo 项目中集成谷歌身份验证的指南。


你可以在你的 Expo 应用中使用以下任一库集成 Google 身份验证:

🌐 You can integrate Google authentication in your Expo app using either of the following libraries:

这两个库都提供原生的登录按钮,并支持对用户进行身份验证(并获取使用 Google API 的授权)。由于它们需要自定义原生代码,你需要在应用配置中使用配置插件并构建开发版本。

🌐 Both libraries provide native sign-in buttons and support authenticating the user (and obtaining authorization to use Google APIs). Because they require custom native code, you'll need to use a config plugin in the app config and build a development build.

选择库

🌐 Choosing a library

在两者库之间进行选择时,请考虑以下差异:

🌐 When choosing between the two libraries, consider the following differences:

  • react-native-nitro-google-signin 包含对 Android 凭证管理器 的支持。
  • @react-native-google-signin/google-signin 提供 作为其付费服务的一部分的 Android 凭证管理 API

警告 适用于 Android 的传统 Google 登录 SDK(com.google.android.gms:play-services-auth 的一部分)已被弃用,Google 建议迁移到 Android 凭据管理器。参见 关于从传统 Google 登录迁移

本指南提供了有关如何为你的项目配置库的信息。

🌐 This guide provides information on how to configure the library for your project.

先决条件

1 要求

开发版本

这些库不能在 Expo Go 中使用,因为它们需要自定义原生代码。了解更多关于向你的应用添加自定义原生代码的信息。

安装

🌐 Installation

根据你想使用的库选择安装指南:

🌐 Choose an installation guide based on the library you want to use:

React Native Nitro 谷歌登录
React Native Google 登录:Expo 安装说明

为 Android 和 iOS 配置 Google 项目

🌐 Configure Google project for Android and iOS

以下是有关如何为 Android 和 iOS 配置 Google 项目的说明。

🌐 Below are instructions on how to configure your Google project for Android and iOS.

将应用上传到 Google Play 商店

🌐 Upload app to Google Play Store

如果你的应用打算在生产环境中运行,我们建议将应用上传到 Google Play 商店。即使你的项目仍在开发中,你也可以将应用提交到商店进行测试。这样,你就可以在应用通过 EAS 签名进行测试时,以及通过 Google Play 应用签名 进行商店部署时测试 Google 登录功能。要了解更多关于应用提交流程的信息,请按以下指定顺序查看下列指南:

🌐 We recommend uploading the app to the Google Play Store if your app intends to run in production. You can submit your app to the stores for testing even if your project is still in development. This allows you to test Google Sign In when your app is signed by EAS for testing, and when it is signed by Google Play App Signing for store deployment. To learn more about the app submission process, see the guides below in the order they are specified:

创建你的第一个 EAS 构建
为应用商店构建你的项目
首次手动上传 Android 应用

配置你的 Firebase 或 Google Cloud Console 项目

🌐 Configure your Firebase or Google Cloud Console project

请参阅库文档以获取更深入的配置指南:

🌐 Refer to the library documentation for a more in-depth configuration guide:

对于 Android,一旦你上传了应用,在使用 Firebase 或 Google Cloud 控制台配置项目时,需要提供 SHA-1 证书指纹值。有两种类型的值可以提供:

🌐 For Android, once you have uploaded your app, you need to provide the SHA-1 certificate fingerprint values when asked while configuring the project in Firebase or Google Cloud Console. There are two types of values that you can provide:

  • 你构建的 .apk 的指纹(在你的机器上或使用 EAS Build 构建)。你可以在 Google Play 控制台的 发布 > 设置 > 应用完整性 > 上传密钥证书 下找到 SHA-1 证书指纹。
  • 从 Play 商店下载的生产应用的指纹。你可以在 Google Play 控制台的 发布 > 设置 > 应用完整性 > 应用签名密钥证书 中找到 SHA-1 证书指纹。

使用 Firebase

🌐 With Firebase

有关如何使用 Firebase 配置你的 Android 和 iOS 项目的更多说明:

🌐 For more instructions on how to configure your project for Android and iOS with Firebase:

Firebase(Nitro 谷歌登录)
Firebase(@react-native-google-signin/google-signin)

将 google-services.json 和 GoogleService-Info.plist 上传到 EAS

🌐 Upload google-services.json and GoogleService-Info.plist to EAS

如果你使用适用于 Android 和 iOS 的 Firebase 方法(如上文各节所述),你需要确保 google-services.jsonGoogleService-Info.plist 可在 EAS 中用于构建应用。你可以将它们提交到你的代码仓库,因为这些文件不应包含敏感信息;或者你可以将这些文件作为秘密文件处理,添加到 .gitignore 中,并使用以下指南使它们在 EAS 中可用。

🌐 If you use the Firebase method for Android and iOS (as shared in sections above), you'll need to make sure google-services.json and GoogleService-Info.plist are available in EAS for building the app. You can check them into your repository because the files should not contain sensitive values, or you can treat the files as secrets, add them to .gitignore and use the guide below to make them available in EAS.

将密钥文件上传到 EAS 并在应用配置中使用

使用 Google Cloud Console

🌐 With Google Cloud Console

这是在不使用 Firebase 时配置 Google 项目的另一种方法。

🌐 This is an alternate method to configure a Google project when you are not using Firebase.

有关如何使用 Google Cloud Console 配置你的 Google Android 和 iOS 项目的更多说明:

🌐 For more instructions on how to configure your Google project Android and iOS with Google Cloud Console:

不使用 Firebase 的 Expo(Nitro Google 登录)
不使用 Firebase 的 Expo (@react-native-google-signin/google-signin)