首页 > 解决方案 > 谷歌玩游戏身份验证与 Firebase 无法在 Unity 上运行

问题描述

我已按照官方说明https://firebase.google.com/docs/auth/unity/play-games在 Unity 上使用 firebase 配置 google play 游戏身份验证,但不幸的是它不起作用。我认为当它试图从玩游戏中请求身份验证 ID 时,身份验证过程失败了。这里是来自 logcat 的日志:

12-08 16:27:12.665  2665  2695 I Unity   : GPGClient:Start()
12-08 16:27:12.665  2665  2695 I Unity   :  
12-08 16:27:12.665  2665  2695 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
12-08 16:27:12.665  2665  2695 I Unity   : 
12-08 16:27:18.980  2665  2695 I Unity   : authcode  
12-08 16:27:18.980  2665  2695 I Unity   : GPGClient:SignIn()
12-08 16:27:18.980  2665  2695 I Unity   :  
12-08 16:27:18.980  2665  2695 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
12-08 16:27:18.980  2665  2695 I Unity   : 
12-08 16:27:18.990  2665  2695 I Unity   : punto0
12-08 16:27:18.990  2665  2695 I Unity   : GPGClient:SignIn()
12-08 16:27:18.990  2665  2695 I Unity   : 
12-08 16:27:19.003  2665  2695 I Unity   : Starting Auth with token client.
12-08 16:27:19.003  2665  2695 I Unity   : GooglePlayGames.Android.AndroidClient:Authenticate(Action`2, Boolean)
12-08 16:27:19.003  2665  2695 I Unity   : GPGClient:SignIn()
12-08 16:27:19.003  2665  2695 I Unity   : 
12-08 16:27:19.010  2665  2695 I Unity   : punto1
12-08 16:27:19.343  2665  2695 W Unity   : !!! [Play Games Plugin DLL] 12/08/19 16:27:19 +01:00 WARNING: Creating new PlayGamesPlatform
12-08 16:27:19.343  2665  2695 W Unity   : System.Action:Invoke()
12-08 16:27:19.343  2665  2695 W Unity   : GooglePlayGames.OurUtils.PlayGamesHelperObject:Update()
12-08 16:27:19.343  2665  2695 W Unity   :  
12-08 16:27:19.343  2665  2695 W Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
12-08 16:27:19.343  2665  2695 W Unity   : 
12-08 16:27:19.345  2665  2695 I Unity   :  [Play Games Plugin DLL] 12/08/19 16:27:19 +01:00 DEBUG: Activating PlayGamesPlatform.
12-08 16:27:19.345  2665  2695 I Unity   : System.Action:Invoke()
12-08 16:27:19.345  2665  2695 I Unity   : GooglePlayGames.OurUtils.PlayGamesHelperObject:Update()
12-08 16:27:19.345  2665  2695 I Unity   :  
12-08 16:27:19.345  2665  2695 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
12-08 16:27:19.345  2665  2695 I Unity   : 
12-08 16:27:19.347  2665  2695 I Unity   :  [Play Games Plugin DLL] 12/08/19 16:27:19 +01:00 DEBUG: PlayGamesPlatform activated: GooglePlayGames.PlayGamesPlatform
12-08 16:27:19.347  2665  2695 I Unity   : System.Action:Invoke()
12-08 16:27:19.347  2665  2695 I Unity   : GooglePlayGames.OurUtils.PlayGamesHelperObject:Update()
12-08 16:27:19.347  2665  2695 I Unity   :  
12-08 16:27:19.347  2665  2695 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
12-08 16:27:19.347  2665  2695 I Unity   : 
12-08 16:27:19.349  2665  2695 I Unity   :  [Play Games Plugin DLL] 12/08/19 16:27:19 +01:00 DEBUG: Creating platform-specific Play Games client.
12-08 16:27:19.349  2665  2695 I Unity   : System.Action:Invoke()
12-08 16:27:19.349  2665  2695 I Unity   : GooglePlayGames.OurUtils.PlayGamesHelperObject:Update()
12-08 16:27:19.349  2665  2695 I Unity   :  
12-08 16:27:19.349  2665  2695 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
12-08 16:27:19.349  2665  2695 I Unity   : 
12-08 16:27:19.351  2665  2695 I Unity   :  [Play Games Plugin DLL] 12/08/19 16:27:19 +01:00 DEBUG: Creating Android IPlayGamesClient Client
12-08 16:27:19.351  2665  2695 I Unity   : System.Action:Invoke()
12-08 16:27:19.351  2665  2695 I Unity   : GooglePlayGames.OurUtils.PlayGamesHelperObject:Update()
12-08 16:27:19.351  2665  2695 I Unity   :  
12-08 16:27:19.351  2665  2695 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
12-08 16:27:19.351  2665  2695 I Unity   : 
12-08 16:27:35.648  2665  2695 I Unity   : authentication message Authentication canceled
12-08 16:27:35.648  2665  2695 I Unity   : GPGClient:<SignIn>b__4_0(Boolean, String)
12-08 16:27:35.648  2665  2695 I Unity   : System.Action`2:Invoke(T1, T2)
12-08 16:27:35.648  2665  2695 I Unity   : System.Action:Invoke()
12-08 16:27:35.648  2665  2695 I Unity   : GooglePlayGames.OurUtils.PlayGamesHelperObject:Update()
12-08 16:27:35.648  2665  2695 I Unity   :  
12-08 16:27:35.648  2665  2695 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
12-08 16:27:35.648  2665  2695 I Unity   : 
12-08 16:27:35.652  2665  2695 I Unity   : not yet autenticated
12-08 16:27:35.652  2665  2695 I Unity   : System.Action`2:Invoke(T1, T2)
12-08 16:27:35.652  2665  2695 I Unity   : System.Action:Invoke()
12-08 16:27:35.652  2665  2695 I Unity   : GooglePlayGames.OurUtils.PlayGamesHelperObject:Update()
12-08 16:27:35.652  2665  2695 I Unity   :  
12-08 16:27:35.652  2665  2695 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
12-08 16:27:35.652  2665  2695 I Unity   : 
12-08 16:27:35.654  2665  2695 I Unity   :  [Play Games Plugin DLL] 12/08/19 16:27:35 +01:00 DEBUG: Authentication canceled
12-08 16:27:35.654  2665  2695 I Unity   : System.Action:Invoke()
12-08 16:27:35.654  2665  2695 I Unity   : GooglePlayGames.OurUtils.PlayGamesHelperObject:Update()
12-08 16:27:35.654  2665  2695 I Unity   :  
12-08 16:27:35.654  2665  2695 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
12-08 16:27:35.654  2665  2695 I Unity   : 
12-08 16:27:35.672  2665  2695 I Unity   :  [Play Games Plugin DLL] 12/08/19 16:27:35 +01:00 DEBUG: Invoking user callback on game thread
12-08 16:27:35.672  2665  2695 I Unity   : System.Action:Invoke()
12-08 16:27:35.672  2665  2695 I Unity   : GooglePlayGames.OurUtils.PlayGamesHelperObject:Update()
12-08 16:27:35.672  2665  2695 I Unity   :  
12-08 16:27:35.672  2665  2695 I Unity   : (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
12-08 16:27:35.672  2665  2695 I Unity   : 

这里的代码:

using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine.SocialPlatforms;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using Firebase.Auth;

public class GPGClient : MonoBehaviour
{
    private string authCode;
    private FirebaseAuth auth;
    public Text UserID;

    private void Start()
    {
        Debug.Log("start");
        UserID.text = "user";
        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
            .RequestServerAuthCode(false /* Don't force refresh */)
            // requests an ID token be generated.  This OAuth token can be used to
            //  identify the player to other services such as Firebase.
            .RequestIdToken()
            .Build();

        PlayGamesPlatform.DebugLogEnabled = true;
        PlayGamesPlatform.InitializeInstance(config);
        PlayGamesPlatform.Activate();

    }

    public void SignIn()
    {
        authCode = PlayGamesPlatform.Instance.GetServerAuthCode();
        Debug.Log("authcode  " + authCode);
        Debug.Log("punt
    {
        authCode = PlayGamesPlatform.Instance.GetServerAuthCode();
        Debug.Log("authcode  " + authCode);
        Debug.Log("punto0");
        // Google Play Games authentication.
        Social.localUser.Authenticate((success, message) =>
        {
            Debug.Log("authentication message " + message);
            if (success) 
            {
                //authCode = PlayGamesPlatform.Instance.GetServerAuthCode();
                Debug.Log("autenticated");
                GoOnSignIn();
            }
            else
                Debug.Log("not yet autenticated");


        });



        Debug.Log("punto1");



    }

    private void GoOnSignIn() 
    {
        Debug.Log("punto1.1");
        // Firebase authentication.
        auth = FirebaseAuth.DefaultInstance;

        Credential credential =
            PlayGamesAuthProvider.GetCredential(authCode);
        Debug.Log("punto1.2");
        auth.SignInWithCredentialAsync(credential).ContinueWith(task =>
        {
            Debug.Log("punto2");
            if (task.IsCanceled)
            {
                Debug.Log("punto2cancel");
                UserID.text = "SignInWithCredentialAsync was canceled.";
                return;
            }
            if (task.IsFaulted)
            {
                Debug.Log("punto2fault");
                UserID.text = "SignInWithCredentialAsync encountered an error: " + task.Exception;
                return;
            }
            Debug.Log("punto3");
            FirebaseUser newUser = task.Result;
            UserID.text = "User signed in successfully: {0} ({1})" +
                newUser.DisplayName + newUser.UserId;
        });

        CreateUser();
    }

希望有人能帮助我!谢谢!

标签: firebaseunity3dauthenticationfirebase-authenticationgoogle-play-games

解决方案


推荐阅读