Cant create a lobby: HTTP/1.1 401 Unauthorized

Hi, I’ve just started looking into the new lobby system. When trying to create a lobby, following the example in the documentation, I receive the error (catch exception) “Unity.Services.Lobbies.LobbyServiceException: HTTP/1.1 401 Unauthorized”

Not sure If I’ve forgotten to enable something or have done something dumb, just struggling to sink my teeth into how the new lobby system works.

Appreciate it if someone could point me in the right direction. :slight_smile:

using System;
using System.Collections.Generic;
using Unity.Services.Authentication;
using Unity.Services.Core;
using Unity.Services.Core.Environments;
using Unity.Services.Lobbies;
using Unity.Services.Lobbies.Models;
using UnityEngine;

public class LobbyManager : MonoBehaviour
{
    public string environment = "production";
    async void Start()
    {
        await UnityServices.InitializeAsync();

        try
        {
            string lobbyName = "Custom Game Lobby";
            int maxPlayers = 16;

            // Lobby options
            CreateLobbyOptions options = new CreateLobbyOptions();
            options.IsPrivate = false;

            // lobby data
            options.Data = new Dictionary<string, DataObject>();
            options.Data.Add("Gamemode", new DataObject(DataObject.VisibilityOptions.Public, "Race", DataObject.IndexOptions.S1));

            //lobby player
            string playerId = AuthenticationService.Instance.PlayerId;
            Dictionary<string, PlayerDataObject> playerData = new Dictionary<string, PlayerDataObject>();
            playerData.Add("Name", new PlayerDataObject(PlayerDataObject.VisibilityOptions.Public, "Steve"));
           
            Player player = new Player(playerId, null, playerData);

            options.Player = player;

            Lobby lobby = await Lobbies.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options);

        }
        catch (Exception ex)
        {
            Debug.LogError(ex);
        }

    }

   
}

Error:
Unity.Services.Lobbies.LobbyServiceException: HTTP/1.1 401 Unauthorized —> Unity.Services.Lobbies.Http.HttpException: HTTP/1.1 401 Unauthorized
at Unity.Services.Lobbies.Http.ResponseHandler.HandleAsyncResponse (Unity.Services.Lobbies.Http.HttpClientResponse response, System.Collections.Generic.Dictionary2[TKey,TValue] statusCodeToTypeMap) [0x00066] in C:\Unity Projects\Table Top Racers\Library\PackageCache\com.unity.services.lobby@1.0.0-pre.6\Runtime\Http\ResponseHandler.cs:101 at Unity.Services.Lobbies.Http.ResponseHandler.HandleAsyncResponse[T] (Unity.Services.Lobbies.Http.HttpClientResponse response, System.Collections.Generic.Dictionary2[TKey,TValue] statusCodeToTypeMap) [0x00000] in C:\Unity Projects\Table Top Racers\Library\PackageCache\com.unity.services.lobby@1.0.0-pre.6\Runtime\Http\ResponseHandler.cs:186
at Unity.Services.Lobbies.Apis.Lobby.LobbyApiClient.CreateLobbyAsync (Unity.Services.Lobbies.Lobby.CreateLobbyRequest request, Unity.Services.Lobbies.Configuration operationConfiguration) [0x0011f] in C:\Unity Projects\Table Top Racers\Library\PackageCache\com.unity.services.lobby@1.0.0-pre.6\Runtime\Apis\LobbyApi.cs:224
at Unity.Services.Lobbies.WrappedLobbyService.TryCatchRequest[TRequest,TReturn] (System.Func3[T1,T2,TResult] func, TRequest request) [0x0003a] in C:\Unity Projects\Table Top Racers\Library\PackageCache\com.unity.services.lobby@1.0.0-pre.6\Runtime\SDK\WrappedLobbyService.cs:194 --- End of inner exception stack trace --- at Unity.Services.Lobbies.WrappedLobbyService.ResolveErrorWrapping (Unity.Services.Lobbies.LobbyExceptionReason reason, System.Exception exception) [0x00032] in C:\Unity Projects\Table Top Racers\Library\PackageCache\com.unity.services.lobby@1.0.0-pre.6\Runtime\SDK\WrappedLobbyService.cs:249 at Unity.Services.Lobbies.WrappedLobbyService.TryCatchRequest[TRequest,TReturn] (System.Func3[T1,T2,TResult] func, TRequest request) [0x0011f] in C:\Unity Projects\Table Top Racers\Library\PackageCache\com.unity.services.lobby@1.0.0-pre.6\Runtime\SDK\WrappedLobbyService.cs:221
at Unity.Services.Lobbies.WrappedLobbyService.CreateLobbyAsync (System.String lobbyName, System.Int32 maxPlayers, Unity.Services.Lobbies.CreateLobbyOptions options) [0x00098] in C:\Unity Projects\Table Top Racers\Library\PackageCache\com.unity.services.lobby@1.0.0-pre.6\Runtime\SDK\WrappedLobbyService.cs:39
at LobbyManager.Start () [0x0011b] in C:\Unity Projects\Table Top Racers\Assets\Scripts\LobbyManager.cs:39
UnityEngine.Debug:LogError (object)
LobbyManager/d__1:MoveNext () (at Assets/Scripts/LobbyManager.cs:44)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<Unity.Services.Lobbies.Models.Lobby>:SetException (System.Exception) Unity.Services.Lobbies.WrappedLobbyService/<CreateLobbyAsync>d__4:MoveNext () (at Library/PackageCache/com.unity.services.lobby@1.0.0-pre.6/Runtime/SDK/WrappedLobbyService.cs:40) System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<Unity.Services.Lobbies.Response1<Unity.Services.Lobbies.Models.Lobby>>:SetException (System.Exception) Unity.Services.Lobbies.WrappedLobbyService/<TryCatchRequest>d__182<Unity.Services.Lobbies.Lobby.CreateLobbyRequest, Unity.Services.Lobbies.Models.Lobby>:MoveNext () (at Library/PackageCache/com.unity.services.lobby@1.0.0-pre.6/Runtime/SDK/WrappedLobbyService.cs:228)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<Unity.Services.Lobbies.Response1<Unity.Services.Lobbies.Models.Lobby>>:SetException (System.Exception)
Unity.Services.Lobbies.Apis.Lobby.LobbyApiClient/d__6:MoveNext () (at Library/PackageCache/com.unity.services.lobby@1.0.0-pre.6/Runtime/Apis/LobbyApi.cs:225)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<Unity.Services.Lobbies.Http.HttpClientResponse>:SetResult (Unity.Services.Lobbies.Http.HttpClientResponse) Unity.Services.Lobbies.Http.HttpClient/<MakeRequestAsync>d__3:MoveNext () (at Library/PackageCache/com.unity.services.lobby@1.0.0-pre.6/Runtime/Http/HttpClient.cs:47) System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<Unity.Services.Lobbies.Http.HttpClientResponse>:SetResult (Unity.Services.Lobbies.Http.HttpClientResponse)
Unity.Services.Lobbies.Http.HttpClient/d__7:MoveNext () (at Library/PackageCache/com.unity.services.lobby@1.0.0-pre.6/Runtime/Http/HttpClient.cs:138)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder1<Unity.Services.Lobbies.Http.HttpClientResponse>:SetResult (Unity.Services.Lobbies.Http.HttpClientResponse) Unity.Services.Lobbies.Http.HttpClient/<>c__DisplayClass7_0/<<CreateWebRequestAsync>b__0>d:MoveNext () (at Library/PackageCache/com.unity.services.lobby@1.0.0-pre.6/Runtime/Http/HttpClient.cs:135) System.Threading.Tasks.TaskCompletionSource1<Unity.Services.Lobbies.Http.HttpClientResponse>:SetResult (Unity.Services.Lobbies.Http.HttpClientResponse)
Unity.Services.Lobbies.Http.UnityWebRequestHelpers/<>c__DisplayClass0_0:b__0 (UnityEngine.AsyncOperation) (at Library/PackageCache/com.unity.services.lobby@1.0.0-pre.6/Runtime/Http/UnityWebRequestHelpers.cs:34)
UnityEngine.AsyncOperation:InvokeCompletionEvent ()

1 Like

Hi COZITIME!
I was able to reproduce the Unauthorized response using your code. In order to call into Gaming Services you’ll need to sign in a player. Right now, when I log AuthenticationService.Instance.PlayerId it shows as null. In order to get a valid playerId, you’ll need to use a SignIn method on the AuthenticationService. If you want to make progress immediately, you can sign in an anonymous player. Its looks something like this

await AuthenticationService.Instance.SignInAnonymouslyAsync();

Throw this line somewhere above your reference to AuthenticationService.Instance.PlayerId and the playerId should now be populated (I recommend logging it for debugging reference) and the calls to the Lobby Service should start working!

Note - While testing we found a small issue where authenticated calls to Lobby Service occasionally return a Forbidden (403). This only happens if you’ve recently toggled the service on and only lasts for 1 hour following enablement. We’re working on a fix to reduce the toggle time shortly.

3 Likes

Thank’s you’ve saved me :slight_smile:

1 Like

After following this thread as this was the same issue I had, your little note helped a lot as I was so clueless… Thanks for the info!

1 Like