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}");
}
}