Reputation: 11
I keep receiving errors:
The requested operation is not available. If you continue to experience problems, please contact your administrator
I was provided following details
I want to retrieve data from given api, but I think I need an access token. Based on this solution, I write this C# code:
static async Task<string> GetAccessToken()
{
var TokenUrl = "https://example.com/learning/oauth/token"; // Token URL
var ClientID = "..."; // Client ID
var ClientSecret = "..."; // Client Secret
var Encoded = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ClientID}:{ClientSecret}"));
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Encoded);
var content = new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await client.PostAsync(TokenUrl, content);
if (!response.IsSuccessStatusCode)
{
string errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine(errorContent);
throw new Exception($"HTTP error! status: {response.StatusCode}");
}
var Response = await response.Content.ReadAsStringAsync();
return Response;
}
Upvotes: 1
Views: 325