gpt4 book ai didi

c# - 场景变化时鉴权失败

转载 作者:行者123 更新时间:2023-11-30 21:29:22 32 4
gpt4 key购买 nike

我设置了我的 firebase 身份验证,它运行良好。但是,当我加载不同的场景并返回欢迎场景后,身份验证失败。场景切换时如何重新授权或保持登录?

我的授权欢迎场景代码:

public void Start()
{
InitializeFirebase();
InitializePlayGamesPlatform();
SignInPlayGames();
}

public void InitializeFirebase()
{
Debug.Log("UserManager: Setting up Firebase Auth");
auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.StateChanged += AuthStateChanged;
auth.IdTokenChanged += IdTokenChanged;
// Specify valid options to construct a secondary authentication object.
if (otherAuthOptions != null &&
!(String.IsNullOrEmpty(otherAuthOptions.ApiKey) ||
String.IsNullOrEmpty(otherAuthOptions.AppId) ||
String.IsNullOrEmpty(otherAuthOptions.ProjectId)))
{
try
{
otherAuth = Firebase.Auth.FirebaseAuth.GetAuth(Firebase.FirebaseApp.Create(
otherAuthOptions, "Secondary"));
otherAuth.StateChanged += AuthStateChanged;
otherAuth.IdTokenChanged += IdTokenChanged;
}
catch (Exception)
{
Debug.Log("UserManager: ERROR: Failed to initialize secondary authentication object.");
}
}
AuthStateChanged(this, null);
}

public void InitializePlayGamesPlatform()
{
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
.RequestServerAuthCode(false)
.Build();

PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.Activate();

auth = FirebaseAuth.DefaultInstance;
}

public void SignInPlayGames()
{
connecting = true;
ServerConnectionStatus.GetComponent<Text>().text = "Connecting to PlayGames";
ServerConnectionStatus.GetComponent<Text>().color = Color.black;

Social.localUser.Authenticate((bool success) => {
if (!success)
{
Debug.LogError("UserManager: Failed to Sign in into PlayGames Service");
ServerConnectionStatus.GetComponent<Text>().text = "Cannot Connect to PlayGames";
ServerConnectionStatus.GetComponent<Text>().color = Color.red;
connecting = false;
return;
}

AuthCode = PlayGamesPlatform.Instance.GetServerAuthCode();

if (string.IsNullOrEmpty(AuthCode))
{
Debug.LogError("UserManager: Signed in into PlayGames Service, Failed to get Server Auth Code");
ServerConnectionStatus.GetComponent<Text>().text = "Cannot Connect to PlayGames";
ServerConnectionStatus.GetComponent<Text>().color = Color.red;
connecting = false;
return;
}

Debug.LogFormat("UserManager: Server Auth Code = {0}", AuthCode);

Credential credential = PlayGamesAuthProvider.GetCredential(AuthCode);
auth.SignInWithCredentialAsync(credential).ContinueWith(task => {
if (task.IsCanceled)
{
Debug.LogError("UserManager: SignInWithCredentialAsync was canceled.");
ServerConnectionStatus.GetComponent<Text>().text = "Cannot Sign in to PlayGames";
ServerConnectionStatus.GetComponent<Text>().color = Color.red;
task.Exception.ToString();
connecting = false;
return;
}
if (task.IsFaulted)
{
Debug.LogError("UserManager: SignInWithCredentialAsync encountered an error: " + task.Exception);
ServerConnectionStatus.GetComponent<Text>().text = "Cannot Sign in to PlayGames";
ServerConnectionStatus.GetComponent<Text>().color = Color.red;
connecting = false;
return;
}

user = task.Result;
Debug.LogFormat("UserManager: User signed in successfully: {0} ({1})",
user.DisplayName, user.UserId);
ServerConnectionStatus.GetComponent<Text>().text = "Connected to PlayGames";
ServerConnectionStatus.GetComponent<Text>().color = Color.green;
connected = true;
connecting = false;
nick = auth.CurrentUser.DisplayName;
SetPlayerName(nick);
});
});
}

public void SetPlayerName(string value)
{
PhotonNetwork.NickName = value;
PlayerPrefs.SetString("PlayerName", value);
}

void AuthStateChanged(object sender, System.EventArgs eventArgs)
{
Firebase.Auth.FirebaseAuth senderAuth = sender as Firebase.Auth.FirebaseAuth;
Firebase.Auth.FirebaseUser user = null;
if (senderAuth != null) userByAuth.TryGetValue(senderAuth.App.Name, out user);
if (senderAuth == auth && senderAuth.CurrentUser != user)
{
bool signedIn = user != senderAuth.CurrentUser && senderAuth.CurrentUser != null;
if (!signedIn && user != null)
{
Debug.Log("UserManager: Signed out " + user.UserId);
}
user = senderAuth.CurrentUser;
userByAuth[senderAuth.App.Name] = user;
if (signedIn)
{
Debug.Log("UserManager: Signed in " + user.UserId);
displayName = user.DisplayName ?? "";
DisplayDetailedUserInfo(user, 1);
}
}
}

结果错误:

04-01 01:37:41.330: E/Unity(6065): UserManager: SignInWithCredentialAsync encountered an error: System.AggregateException: One or more errors occurred.

---> System.AggregateException: One or more errors occurred.

---> Firebase.FirebaseException: The supplied auth credential is malformed or has expired. [ Error getting access token from playgames.google.com, OAuth2 redirect uri is: http://localhost, response: OAuth2TokenResponse{params: error=invalid_grant&error_description=Bad%20Request, httpMetadata: HttpMetadata{status=400, cachePolicy=NO_CACHE, cacheDuration=null, cacheImmutable=false, staleWhileRevalidate=null, filename=null, lastModified=null, headers=HTTP/1.1 200 OK

最佳答案

我尝试了很多但我没有得到想要的行为(关于重新授权/应用程序不退出)

即使我改变场景,凭据仍然存在。

如果您想更改帐户,

重新启动应用程序,然后在欢迎场景中注销(facebook 或 google)

关于c# - 场景变化时鉴权失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55446118/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com