Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Update a player from anonymous to a Unity Player Account

Discussion in 'Authentication' started by gamesdeveloper2, Sep 16, 2023.

  1. gamesdeveloper2

    gamesdeveloper2

    Joined:
    Feb 2, 2021
    Posts:
    1
    Hi

    I want to do an update a player from anonymous to a Unity Player Account, my code is this:

    public async void SignInAnonymous()
    {
    AuthenticationService.Instance.ClearSessionToken();
    await SignInAnonymouslyAsync();
    }
    async Task SignInAnonymouslyAsync()
    {
    try
    {
    await AuthenticationService.Instance.SignInAnonymouslyAsync();
    Debug.Log("Sign in anonymously succeeded!");
    // Shows how to get the playerID
    Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");

    }
    catch (AuthenticationException ex)
    {
    // Compare error code to AuthenticationErrorCodes
    // Notify the player with the proper error message
    Debug.LogException(ex);
    }
    catch (RequestFailedException ex)
    {
    // Compare error code to CommonErrorCodes
    // Notify the player with the proper error message
    Debug.LogException(ex);
    }
    }


    public async void UpdateFromAnonymousToUnityPlayerAccount()
    {
    await PlayerAccountService.Instance.StartSignInAsync();
    await LinkWithUnityAsync(PlayerAccountService.Instance.AccessToken);
    }
    async Task LinkWithUnityAsync(string accessToken)
    {
    try
    {
    await AuthenticationService.Instance.LinkWithUnityAsync(accessToken);
    Debug.Log("Link is successful.");
    }
    catch (AuthenticationException ex) when (ex.ErrorCode == AuthenticationErrorCodes.AccountAlreadyLinked)
    {
    // Prompt the player with an error message.
    Debug.LogError("This user is already linked with another account. Log in instead.");
    }
    catch (AuthenticationException ex)
    {
    // Compare error code to AuthenticationErrorCodes
    // Notify the player with the proper error message
    Debug.LogException(ex);
    }
    catch (RequestFailedException ex)
    {
    // Compare error code to CommonErrorCodes
    // Notify the player with the proper error message
    Debug.LogException(ex);
    }
    }

    But I get this error:

    [Authentication]: Request failed: 400, {"title":"INVALID_PARAMETERS","detail":"external token not provided","details":[],"status":400}, request-id: 63bf303f-c23e-4bc4-a3c6-a467b111cbf8
    UnityEngine.Logger:Log (string,object)
    Unity.Services.Authentication.Logger:Log (object) (at Library/PackageCache/com.unity.services.authentication@3.0.0/Runtime/Utilities/Logger.cs:16)
    Unity.Services.Authentication.AuthenticationExceptionHandler:ConvertException (Unity.Services.Authentication.WebRequestException) (at Library/PackageCache/com.unity.services.authentication@3.0.0/Runtime/Exceptions/AuthenticationExceptionHandler.cs:104)
    Unity.Services.Authentication.AuthenticationServiceInternal/<LinkWithExternalTokenAsync>d__153:MoveNext () (at Library/PackageCache/com.unity.services.authentication@3.0.0/Runtime/AuthenticationServiceInternal.cs:825)
    System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<Unity.Services.Authentication.LinkResponse>:SetException (System.Exception)
    Unity.Services.Authentication.WebRequest/<SendAsync>d__15`1<Unity.Services.Authentication.LinkResponse>:MoveNext () (at Library/PackageCache/com.unity.services.authentication@3.0.0/Runtime/Network/WebRequest.cs:63)
    System.Threading.Tasks.TaskCompletionSource`1<string>:SetException (System.Exception)
    Unity.Services.Authentication.WebRequest:RequestCompleted (System.Threading.Tasks.TaskCompletionSource`1<string>,long,bool,bool,string,string,System.Collections.Generic.IDictionary`2<string, string>) (at Library/PackageCache/com.unity.services.authentication@3.0.0/Runtime/Network/WebRequest.cs:193)
    Unity.Services.Authentication.WebRequest/<>c__DisplayClass16_1:<SendAttemptAsync>b__0 (UnityEngine.AsyncOperation) (at Library/PackageCache/com.unity.services.authentication@3.0.0/Runtime/Network/WebRequest.cs:76)
    UnityEngine.AsyncOperation:InvokeCompletionEvent ()


    upload_2023-9-16_12-49-29.png
     
  2. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,166
    A bit old but I have the same problem.

    Did you solve this problem?

    Also, why do you include AuthenticationService.Instance.ClearSessionToken(); in the beginning?