Backend service AUTH token generation failed

24 views
Skip to first unread message

Darshan Kumar

unread,
May 29, 2024, 4:38:11 AMMay 29
to SMART on FHIR
Hi below error is received when tried to generate auth token through c# code  ,

{
  "error": "invalid_client",
  "error_description": null
}

code 
public static async Task<string> GetAccessToken()
        {
            Console.WriteLine("Creating JWTToken...");
            var jwtToken = generatetoken();
       

            Console.WriteLine("Creating Access Token...");
            var formData = new StringContent(
                $"grant_type=client_credentials&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer&client_assertion={jwtToken}",
                Encoding.UTF8,
                "application/x-www-form-urlencoded");

            // Add necessary headers
            var requestMessage = new HttpRequestMessage(HttpMethod.Post, tokenEndpoint)
            {
                Content = formData
            };
            //requestMessage.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

            var response = await httpClient.SendAsync(requestMessage);
            var responseBody = await response.Content.ReadAsStringAsync();

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine($"Response: {responseBody}");
                throw new Exception($"Failed to retrieve access token. Status code: {response.StatusCode}");
            }

            // Assuming the response is JSON and contains the access token in a field named "access_token"
            var jsonResponse = JObject.Parse(responseBody);
            var accessToken = jsonResponse["access_token"]?.ToString();

            if (string.IsNullOrEmpty(accessToken))
            {
                throw new Exception("Access token not found in the response.");
            }

            Console.WriteLine($"Access token: {accessToken}");
            return accessToken;
        }

Payload is given below
payload.png

            var headerClaims = new
            {
                typ = "JWT",
                alg = "RS384"
            };

I have checked the documnetation and uploaded the private key 2 days back but still getting the issue . Can anyone help me out with this isssue
Reply all
Reply to author
Forward
0 new messages