Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Bug JWT token Unauthorized on very first request made

Discussion in 'Game Server Hosting' started by jackward84, Jul 5, 2023.

  1. jackward84

    jackward84

    Joined:
    Jan 26, 2017
    Posts:
    87
    I am getting a token from the payload APIs as described in the docs here:

    https://services.docs.unity.com/multiplay-localproxy/v1/#tag/Payload/operation/PayloadToken

    Code (CSharp):
    1.         var httpClient = new HttpClient();
    2.         var tokenExchangeUrl = "http://localhost:8086/v1/payload/token";
    3.         var response = await httpClient.GetAsync(tokenExchangeUrl).ConfigureAwait(false);
    4.         var responseText = response.Content.ReadAsStringAsync().Result;
    5.         var json = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseText);
    6.         var accessToken = json["token"];
    7.         Debug.Log("Success getting token");
    8.         UgsRequest.Initialize(accessToken);
    This code fires almost immediately when the server boots and initializes UgsRequest, so that my request helper class has a usable access token to make requests to leaderboards, cloud saves and such.

    The problem I am having, is that no matter what, the very first request that is made with this JWT token gets an unauthorized error. It only happens for the very first request made with it.

    As such, I am able to work around the issue by firing off a dummy request that does nothing at all, as this request will cause all subsequent requests will begin to work.

    Code (CSharp):
    1.     public static void Initialize(string accessToken)
    2.     {
    3.         _accessToken = accessToken;
    4.      
    5.         // shoot out a dummy request to init the access token
    6.         Task.Run(() => Get($"https://leaderboards.services.api.unity.com/v1/projects/{BigRoxConnection.ProjectId}/leaderboards/1v1/scores"));
    7.     }
    This doesn't happen if I use my own service account key and fetch the JWT token manually, everything works fine in that scenario. It's only when I use the built in JWT token exchange.

    For reference, the relevant helper class code:

    Code (CSharp):
    1.     private static HttpClient _httpClient;
    2.     private static string _accessToken;
    3.  
    4.     public static HttpClient HttpClient
    5.     {
    6.         get
    7.         {
    8.             if (_httpClient == null)
    9.             {
    10.                 _httpClient = new HttpClient();
    11.                 _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {_accessToken}");
    12.             }
    13.             return _httpClient;
    14.         }
    15.     }
    16.  
    17.     public static async Task<object> Get(string url, bool notFoundOk = true)
    18.     {
    19.         return await Get<object>(url, notFoundOk).ConfigureAwait(false);
    20.     }
    21.  
    22.     public static async Task<T> Get<T>(string url, bool notFoundOk = true)
    23.     {
    24.         var response = await HttpClient.GetAsync(url).ConfigureAwait(false);
    25.         var result = response.Content.ReadAsStringAsync().Result;
    26.         return HandleResponse<T>(url, response, result, notFoundOk);
    27.     }
     
    Last edited: Jul 5, 2023