You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity安卓游戏Firebase Google登录在Play Console测试版无法完成求助

问题描述

我使用Unity开发安卓游戏,已成功实现Firebase Google登录功能,本地构建的APK文件可正常完成登录流程。但通过Google Play Console上传的测试版本无法完成登录:选择Google账号的弹窗能够弹出,但登录流程无法继续推进。请告知需要排查的要点,以下是我使用的代码:

排查要点
  • Google Cloud Console OAuth配置
    • 确认已将Google Play Console生成的发布签名证书SHA-1添加到Google Cloud Console的OAuth 2.0客户端ID配置中(本地APK使用调试签名,发布版本使用Google Play提供的签名,两者SHA-1不同)
    • 验证代码中webClientId与Google Cloud Console中创建的Web客户端ID完全一致
  • Firebase项目签名配置
    • 在Firebase控制台的「项目设置」→「应用」→「安卓应用」中,添加Google Play发布签名的SHA-1指纹
  • Google Play应用签名设置
    • 确认已在Google Play Console启用「应用签名」,并从「应用完整性」→「应用签名」页面获取正确的发布签名SHA-1
  • 代码异步逻辑与日志
    • 为发布版本添加远程日志(如Firebase Crashlytics),捕获OnAuthenticationFinished和SignInWithGoogleOnFirebase中的异常信息,本地调试日志在发布版本无法直接查看
    • 替换普通ContinueWith为Firebase提供的ContinueWithOnMainThread,确保UI操作和Firebase回调在Unity主线程执行(Unity不允许在子线程操作UI)
  • 权限与依赖
    • 检查AndroidManifest.xml是否包含INTERNET权限,ProGuard/R8混淆规则是否保留了Firebase和Google Sign-In的必要类(可参考官方混淆模板)
    • 确认Firebase Auth和Google Sign-In Unity SDK使用最新稳定兼容版本
  • 测试环境验证
    • 测试账号已添加到Google Play Console的内部/封闭测试列表,且已接受测试邀请
    • 测试设备的Google Play服务为最新版本
代码实现
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Firebase;
using Firebase.Auth;
using Google;
using UnityEditor;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;

public class GoogleSignInClass : MonoBehaviour
{
    public Text infoText;
    public string webClientId = "310848617796-nf5a9ds97humnn8nqv5gihmnnqg6j3g4.apps.googleusercontent.com";

    private FirebaseAuth auth;
    private GoogleSignInConfiguration configuration;
    [SerializeField] private bool IsEditor = false;
    [FormerlySerializedAs("Canvas")] [SerializeField] private MainMenu Canvas; 
    private void Awake()
    {
        configuration = new GoogleSignInConfiguration { WebClientId = webClientId, RequestEmail = true, RequestIdToken = true };
        CheckFirebaseDependencies();
        if (Application.isEditor)
            IsEditor = true;
    }

    Firebase.Auth.FirebaseUser user;

    // Handle initialization of the necessary firebase modules:
    void InitializeFirebase()
    {
        Debug.Log("Setting up Firebase Auth");
        auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
        auth.StateChanged += AuthStateChanged;
        AuthStateChanged(this, null);
    }

    // Track state changes of the auth object.
    void AuthStateChanged(object sender, System.EventArgs eventArgs)
    {
        if (auth.CurrentUser != user)
        {
            bool signedIn = user != auth.CurrentUser && auth.CurrentUser != null;
            if (!signedIn && user != null)
            {
                Debug.Log("Signed out " + user.UserId);
            }
            user = auth.CurrentUser;
            if (signedIn)
            {
                Debug.Log("Signed in " + user.UserId);
            }
        }
    }

    void OnDestroy()
    {
        auth.StateChanged -= AuthStateChanged;
        auth = null;
    }


    private void CheckFirebaseDependencies()
    {
        FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
        {
            if (task.IsCompleted)
            {
                if (task.Result == DependencyStatus.Available)
                    auth = FirebaseAuth.DefaultInstance;
                else
                    AddToInformation("Could not resolve all Firebase dependencies: " + task.Result.ToString());
            }
            else
            {
                AddToInformation("Dependency check was not completed. Error : " + task.Exception.Message);
            }
        });
    }

    public void SignInWithGoogle() { OnSignIn(); }
    public void SignOutFromGoogle() { OnSignOut(); }

    private void OnSignIn()
    {
        if (!IsEditor)
        {
            GoogleSignIn.Configuration = configuration;
            GoogleSignIn.Configuration.UseGameSignIn = false;
            GoogleSignIn.Configuration.RequestIdToken = true;
            AddToInformation("Calling SignIn");

            GoogleSignIn.DefaultInstance.SignIn().ContinueWith(OnAuthenticationFinished);
        }
        else
        {
            Canvas.GoMainMenu();
        }
    }

    private void OnSignOut()
    {
        AddToInformation("Calling SignOut");
        GoogleSignIn.DefaultInstance.SignOut();
    }

    public void OnDisconnect()
    {
        AddToInformation("Calling Disconnect");
        GoogleSignIn.DefaultInstance.Disconnect();
    }

    internal void OnAuthenticationFinished(Task<GoogleSignInUser> task)
    {
        if (task.IsFaulted)
        {
            using (IEnumerator<Exception> enumerator = task.Exception.InnerExceptions.GetEnumerator())
            {
                if (enumerator.MoveNext())
                {
                    GoogleSignIn.SignInException error = (GoogleSignIn.SignInException)enumerator.Current;
                    AddToInformation("Got Error: " + error.Status + " " + error.Message);
                }
                else
                {
                    AddToInformation("Got Unexpected Exception?!?" + task.Exception);
                }
            }
        }
        else if (task.IsCanceled)
        {
            AddToInformation("Canceled");
        }
        else
        {
            AddToInformation("Welcome: " + task.Result.DisplayName + "!");
            AddToInformation("Email = " + task.Result.Email);
            AddToInformation("Email = " + task.Result.Email);
            SignInWithGoogleOnFirebase(task.Result.IdToken, task.Result.UserId);
        }
    }

    private void SignInWithGoogleOnFirebase(string idToken, string UID)
    {
        Credential credential = GoogleAuthProvider.GetCredential(idToken, null);

        auth.SignInWithCredentialAsync(credential).ContinueWith(task =>
        {
            AggregateException ex = task.Exception;
            if (ex != null) 
            {
                if (ex.InnerExceptions[0] is FirebaseException inner && (inner.ErrorCode != 0)) 
                    AddToInformation("\nError code = " + inner.ErrorCode + " Message = " + inner.Message);
            }
            else
            {
                AddToInformation("Sign In Successful., UID: " + UID);
                Canvas.GoMainMenu();
                Player.instance.SetIsSignIn(true);
                Player.instance.setGoogleUID(UID);
                AddToInformation("Success2");
                //StartCoroutine(signInSetPlayer());
            }
        });
    }

   
    public void OnSignInSilently()
    {
        GoogleSignIn.Configuration = configuration;
        GoogleSignIn.Configuration.UseGameSignIn = false;
        GoogleSignIn.Configuration.RequestIdToken = true;
        AddToInformation("Calling SignIn Silently");

        GoogleSignIn.DefaultInstance.SignInSilently().ContinueWith(OnAuthenticationFinished);
    }

    public void OnGamesSignIn()
    {
        GoogleSignIn.Configuration = configuration;
        GoogleSignIn.Configuration.UseGameSignIn = true;
        GoogleSignIn.Configuration.RequestIdToken = false;

        AddToInformation("Calling Games SignIn");

        GoogleSignIn.DefaultInstance.SignIn().ContinueWith(OnAuthenticationFinished);
    }

    private void AddToInformation(string str) { infoText.text += "\n" + str; }
}

内容的提问来源于stack exchange,提问作者성호태

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 17:17:58