APIService and CloudSave

Can anyone show what I’m doing wrong? I’m trying to do a simple anonymous signup, then cloud save an item. First I initialize the services, then create a GameClient, use it to sign up anonymously, then call cloud save with its idToken. Maybe there is some intermediate step I’m missing, but using:
AuthenticationService.Instance.SignInAnonymouslyAsync();
CloudSaveService.Instance.Data.Player.SaveAsync()
does work with what seems like the same fundamental steps

        UnityServices.InitializeAsync();

        var gameClient = ApiService.CreateGameClient();

        var signUpRequest = await gameClient.PlayerAuthentication.SignUpAnonymously(Application.cloudProjectId);
        signUpRequest.EnsureSuccessful();

        // Save an item
        var saveResult = await gameClient.CloudSaveData.SetItem(
            Application.cloudProjectId,
           signUpRequest.Data.UserId,
            new Unity.Services.Apis.CloudSave.SetItemBody("testKey", "testValue")
        );
        saveResult.EnsureSuccessful();

Okay, solution was needing
gameClient.SetAccessToken(signUpRequest.Data.IdToken);
I incorrectly assumed SignUpAnonymous() would automatically auth the gameClient internally

The game clients directly wraps authentication methods that will automatically set the access token. If you use the Player Authentication api directly, this won’t be the case.

  • gameClient.SignUpAnonymously will set the access token with the result
  • gameClient.PlayerAuthentication.SignUpAnonymously will not

This could be more obvious, we will review the documentation and see if we can improve this.

1 Like