“开发人员错误:此应用程序配置错误” Google 使用列入白名单的客户端 ID 在 Firebase 上登录

Tom*_*Tom 3 google-app-engine android firebase firebase-authentication

我正在尝试在我的 android 应用程序上迁移到 firebase auth。包名称和 SHA1 当前与旧的应用引擎项目(我不想升级到 firebase)相关联,因此我无法将 SHA1 指纹添加到我的新 firebase 项目中。App Engine 项目目前正在生产中,因此我无法删除 android 客户端 ID 或删除该项目。此处的文档https://support.google.com/firebase/answer/6401008?authuser=0说我应该能够将旧应用引擎项目的客户端 ID 列入白名单。但是,当我这样做然后尝试使用 google 使用 firebase auth 登录到应用程序时,我收到以下错误"Developer error: this application is misconfigured. Check your SHA1 and package name in the Firebase console."

我还尝试了此处的说明https://firebase.googleblog.com/2016/12/working-with-multiple-firebase-projects-in-an-android-app.html,其中涉及将GoogleSignInOptions对象传入 AuthUI 构建器使用来自相应项目的 Web 客户端 ID,如下所示:

private void StartLoginActivity() {

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken("<my-client-id>.apps.googleusercontent.com")
            .requestEmail()
            .build();

    List<AuthUI.IdpConfig> providers = Arrays.asList(
            new AuthUI.IdpConfig.EmailBuilder().build(),
            new AuthUI.IdpConfig.GoogleBuilder().setSignInOptions(gso).build());

    startActivityForResult(
            AuthUI.getInstance()
                    .createSignInIntentBuilder()
                    .setAvailableProviders(providers)
                    .build(),
            RC_SIGN_IN);
}
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,我得到了错误 java.lang.IllegalArgumentException: two different server client ids provided

所以我的问题是,当 SHA1 与现有 GCP 项目关联时,如何配置我的 firebase 项目以启用身份验证?

Tom*_*Tom 6

我从 firebase 支持那里得到了以下响应,并且能够使其正常工作:

Google 使用 FirebaseUI 登录是通过 google-services.json > 文件配置的。问题是,如果您使用来自不同 > 项目的 OAuth 客户端 ID,则 default_web_client_id 将被错误配置。它将 > 指向您的新 Firebase 项目的自动生成的 Web 客户端 ID;我们>不想要这个。

在https://developers.google.com/identity/sign-in/android/backend- 的第 1 步中,我们调用 #requestIdToken(getString(R.string.server_client_id))。在 >firebaseUI 中,这也完成了,这个资源是“default_web_client_id” - >由 google-services.json 生成。这需要改变。

因此,要解决您需要使用项目 >#1 中的 Web OAuth 客户端 ID 的问题,您可以通过以下任一方式执行此操作:

更改 google-services.json 文件 - 您需要将 client_id >fields 更改为项目 #1 中正确的 Web 客户端 ID 您还需要将 Web 客户端 >OAuth 从项目列入白名单(Auth > Sign In Providers > Google) #1 到项目#2

不使用 google-services.json 以便您可以自己设置 default_web_client_id。我们为此制作了指南,请参见此处。

这是我为使其工作而遵循的过程:

  1. 转到 GCP Console > 选择旧项目 > API 和服务 > 凭据

  2. 创建凭据 > OAuth 客户端 ID

  3. 选择Web 应用程序

    • 输入名称
    • 将 Javascript 来源和授权重定向 URL 留空
  4. 复制客户端ID

  5. 打开 Firebase 控制台

  6. 转到身份验证 > 登录方法 > 谷歌
  7. 将新客户端 ID 列入白名单 > 添加 > 保存
  8. 返回设置 > 将 firebase 添加到您的 android 应用
    • 添加您的android包名称
    • 将签名证书留空
  9. 点击注册应用
  10. 下载并打开 google-services.json
  11. 在 oauth_client 中,您的应用程序中的每个 oauth 客户端 ID 都会有一个条目(仅供参考,这些可以在 GCP 控制台上的 API 和服务 > 凭据下看到)。

  12. 删除整个oauth_client标签并替换为 "oauth_client": [ { "client_id": "<your_new_client_id>", "client_type": 3 } ], If you need any other oauth clients 然后确保列入白名单的一个在顶部,因为这似乎是默认值。

  13. 保存文件并复制到您的 android 应用程序模块中。

  14. 确保您具有以下依赖项:

    • 项目根构建.gradle
    • 类路径 'com.google.gms:google-services:4.0.0'
    • 应用模块 build.gradle
    • 编译 'com.google.firebase:firebase-core:16.0.0'
    • 编译 'com.firebaseui:firebase-ui-auth:4.0.0'
  15. 构建并运行您的项目

如果您收到此错误(没有更多详细信息): BasicNetwork.performRequest: Unexpected response code 400 for https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyAssertion?alt=proto&key=AIzaSyBJL6EO8vMEJpyWUCAKP8ZgH4LYR0Hrwpk

检查您的 gradle 依赖项是否设置为上述版本(或更高版本)

如果您收到此错误: com.google.firebase.FirebaseException: An internal error has occurred. [ Invalid Idp Response:the Google id_token is not allowed to be used with this application. Its audience (OAuth 2.0 client ID) is <your-client-id>, which is not authorized to be used in the project with project_number: <your-project-number>. ]

尝试在 Firebase 控制台上删除并重新添加列入白名单的客户端 ID。