Access Token from Authorisation token

Could you help resolve why my C# method below, it isn’t generating access tokens from the Authorization Token? I keep getting BadRequest with any authorization token I get. See the methods below:

public static async Task<string> GetAccessTokenAsync2(string authorizationToken)
{
    Codes codes = new Codes();
    try
    {
        using (HttpClient _httpClient = new HttpClient())
        {
            var requestUri = "https://zoom.us/oauth/token";
            var request = new HttpRequestMessage(HttpMethod.Post, requestUri);
            MessageBox.Show($"Credentials: {codes.AppClientID} {codes.AppClientSecret}");
            request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{codes.AppClientID}:{codes.AppClientSecret}")));
            request.Content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("code", authorizationToken),
                new KeyValuePair<string, string>("grant_type", "authorization_code"),
                new KeyValuePair<string, string>("redirect_uri", codes.RedirectUrl)
            });
            MessageBox.Show($"Credentials: {authorizationToken} {codes.RedirectUrl}");
            var response = await _httpClient.SendAsync(request);
            MessageBox.Show($"StatusCode: {response.StatusCode}");
            response.EnsureSuccessStatusCode();
            MessageBox.Show($"After ");
            var responseBody = await response.Content.ReadAsStringAsync();
            MessageBox.Show($"1");
            var jsonObject = JsonDocument.Parse(responseBody);

            if (jsonObject.RootElement.TryGetProperty("access_token", out var accessToken))
            {
                return accessToken.ToString();
            }
            else
            {
                // Handle the case where 'access_token' property is missing
                throw new InvalidOperationException("Access token not found in the response.");
            }
        }
    }
    catch (HttpRequestException ex)
    {
        // Handle HTTP request exceptions
        Console.WriteLine($"HTTP Request Exception: {ex.Message}");
        throw new Exception ($"HTTP Request Exception: {ex.Message}");
    }
    catch (System.Text.Json.JsonException ex)
    {
        // Handle JSON parsing exceptions
        Console.WriteLine($"JSON Parsing Exception: {ex.Message}");
        throw new Exception($"JSON Parsing Exception: {ex.Message}");
    }
    catch (Exception ex)
    {
        // Handle other exceptions
        Console.WriteLine($"Exception occurred: {ex.Message}");
        throw new Exception($"Exception occurred: {ex.Message}");
    }
}

@chuxyno I’ve just tested this, and your code works fine.

One important thing to take note is that your AppClientID, AppClientSecret and redirectURL must be correct.

Instead of using localhost for my redirectURL, I’ve used a tunnel service such as ngrok to ensure I have a valid publicly accessible URL.

There is nothing much I’ve changed in the code, except for commenting out some parts, and hiding my secret & clientID.

Are you using an OAuth app by the way?

    String AppClientID = "redacted";
    String AppClientSecret = "redacted";
    String redirectURL = "https://18cc-58-182-81-118.ngrok-free.app/redirectforoauth";

    var authorizationToken = context.Request.Query["code"].ToString();

  
    try
    {
        using (HttpClient _httpClient = new HttpClient())
        {
            var requestUri = "https://zoom.us/oauth/token";
            var request = new HttpRequestMessage(HttpMethod.Post, requestUri);
            //MessageBox.Show($"Credentials: {codes.AppClientID} {codes.AppClientSecret}");
            request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{AppClientID}:{AppClientSecret}")));
            request.Content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("code", authorizationToken),
                new KeyValuePair<string, string>("grant_type", "authorization_code"),
                new KeyValuePair<string, string>("redirect_uri", redirectURL)
            });
            //MessageBox.Show($"Credentials: {authorizationToken} {codes.RedirectUrl}");
            var response = await _httpClient.SendAsync(request);
            //MessageBox.Show($"StatusCode: {response.StatusCode}");
            response.EnsureSuccessStatusCode();
            //MessageBox.Show($"After ");
            var responseBody = await response.Content.ReadAsStringAsync();
            //MessageBox.Show($"1");
            var jsonObject = JsonDocument.Parse(responseBody);

            if (jsonObject.RootElement.TryGetProperty("access_token", out var accessToken))
            {
               //return accessToken.ToString();
            }
            else
            {
                // Handle the case where 'access_token' property is missing
                throw new InvalidOperationException("Access token not found in the response.");
            }
        }
    }
    catch (HttpRequestException ex)
    {
        // Handle HTTP request exceptions
        Console.WriteLine($"HTTP Request Exception: {ex.Message}");
        throw new Exception($"HTTP Request Exception: {ex.Message}");
    }
    catch (System.Text.Json.JsonException ex)
    {
        // Handle JSON parsing exceptions
        Console.WriteLine($"JSON Parsing Exception: {ex.Message}");
        throw new Exception($"JSON Parsing Exception: {ex.Message}");
    }
    catch (Exception ex)
    {
        // Handle other exceptions
        Console.WriteLine($"Exception occurred: {ex.Message}");
        throw new Exception($"Exception occurred: {ex.Message}");
    }

Thanks, Chun, the issue wasn’t caused by this code.

@chuxyno , did you managed to find a solution to this?

Sure, I did. The problem wasn’t from the code. Please close the ticket.