Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Cant create a lobby: HTTP/1.1 401 Unauthorized

Discussion in 'Unity Gaming Services General Discussion' started by COZITIME, Nov 9, 2021.

  1. COZITIME

    COZITIME

    Joined:
    Nov 2, 2016
    Posts:
    2
    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. ^_^

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using Unity.Services.Authentication;
    5. using Unity.Services.Core;
    6. using Unity.Services.Core.Environments;
    7. using Unity.Services.Lobbies;
    8. using Unity.Services.Lobbies.Models;
    9. using UnityEngine;
    10.  
    11. public class LobbyManager : MonoBehaviour
    12. {
    13.     public string environment = "production";
    14.     async void Start()
    15.     {
    16.         await UnityServices.InitializeAsync();
    17.  
    18.         try
    19.         {
    20.             string lobbyName = "Custom Game Lobby";
    21.             int maxPlayers = 16;
    22.  
    23.             // Lobby options
    24.             CreateLobbyOptions options = new CreateLobbyOptions();
    25.             options.IsPrivate = false;
    26.  
    27.             // lobby data
    28.             options.Data = new Dictionary<string, DataObject>();
    29.             options.Data.Add("Gamemode", new DataObject(DataObject.VisibilityOptions.Public, "Race", DataObject.IndexOptions.S1));
    30.  
    31.             //lobby player
    32.             string playerId = AuthenticationService.Instance.PlayerId;
    33.             Dictionary<string, PlayerDataObject> playerData = new Dictionary<string, PlayerDataObject>();
    34.             playerData.Add("Name", new PlayerDataObject(PlayerDataObject.VisibilityOptions.Public, "Steve"));
    35.            
    36.             Player player = new Player(playerId, null, playerData);
    37.  
    38.             options.Player = player;
    39.  
    40.             Lobby lobby = await Lobbies.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options);
    41.  
    42.         }
    43.         catch (Exception ex)
    44.         {
    45.             Debug.LogError(ex);
    46.         }
    47.  
    48.     }
    49.  
    50.    
    51. }
    52.  
    53.  

    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.Dictionary`2[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.Dictionary`2[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.Func`3[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.Func`3[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/<Start>d__1:MoveNext () (at Assets/Scripts/LobbyManager.cs:44)
    System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<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.AsyncTaskMethodBuilder`1<Unity.Services.Lobbies.Response`1<Unity.Services.Lobbies.Models.Lobby>>:SetException (System.Exception)
    Unity.Services.Lobbies.WrappedLobbyService/<TryCatchRequest>d__18`2<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.AsyncTaskMethodBuilder`1<Unity.Services.Lobbies.Response`1<Unity.Services.Lobbies.Models.Lobby>>:SetException (System.Exception)
    Unity.Services.Lobbies.Apis.Lobby.LobbyApiClient/<CreateLobbyAsync>d__6:MoveNext () (at Library/PackageCache/com.unity.services.lobby@1.0.0-pre.6/Runtime/Apis/LobbyApi.cs:225)
    System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<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.AsyncTaskMethodBuilder`1<Unity.Services.Lobbies.Http.HttpClientResponse>:SetResult (Unity.Services.Lobbies.Http.HttpClientResponse)
    Unity.Services.Lobbies.Http.HttpClient/<CreateWebRequestAsync>d__7:MoveNext () (at Library/PackageCache/com.unity.services.lobby@1.0.0-pre.6/Runtime/Http/HttpClient.cs:138)
    System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<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.TaskCompletionSource`1<Unity.Services.Lobbies.Http.HttpClientResponse>:SetResult (Unity.Services.Lobbies.Http.HttpClientResponse)
    Unity.Services.Lobbies.Http.UnityWebRequestHelpers/<>c__DisplayClass0_0:<GetAwaiter>b__0 (UnityEngine.AsyncOperation) (at Library/PackageCache/com.unity.services.lobby@1.0.0-pre.6/Runtime/Http/UnityWebRequestHelpers.cs:34)
    UnityEngine.AsyncOperation:InvokeCompletionEvent ()
     
    CasualT_Bossfight likes this.
  2. calebatwd

    calebatwd

    Unity Technologies

    Joined:
    Jan 4, 2018
    Posts:
    2
    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
    Code (CSharp):
    1. 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

    Code (CSharp):
    1. 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.
     
    Last edited: Nov 9, 2021
    suIly, CasualT_Bossfight and COZITIME like this.
  3. COZITIME

    COZITIME

    Joined:
    Nov 2, 2016
    Posts:
    2
    Thank's you've saved me ^_^
     
    calebatwd likes this.
  4. mothergamer1905

    mothergamer1905

    Joined:
    Sep 25, 2018
    Posts:
    9

    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!
     
    calebatwd likes this.