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


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

    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;
    5.         // shoot out a dummy request to init the access token
    6.         Task.Run(() => Get($"{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;
    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.     }
    17.     public static async Task<object> Get(string url, bool notFoundOk = true)
    18.     {
    19.         return await Get<object>(url, notFoundOk).ConfigureAwait(false);
    20.     }
    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