Tenant not found error while getting token from microsoftonline url

26 Views Asked by At

enter image description here [postman request body]enter image description here I am trying to get token through https://login.microsoftonline.com by passing clientid and client secret, the same request generate token through Postman successfully, but from C# HttpClient, It is giving 'Tenent not Found' error.

static HttpClient client = new HttpClient();
Uri baseUri = new Uri("https://login.microsoftonline.com");
client.BaseAddress = baseUri;
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("Accept", "*/*");

HttpRequestMessage requestMessage;
var authenticationString = $"{DefaultConfig.ClientId}:{DefaultConfig.ClientSecret}";
var base64EncodedAuthenticationString = 
Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(authenticationString));

requestMessage = new HttpRequestMessage(HttpMethod.Post, "xxxxxxxx-xxxx-xxxx-xxxx- 
xxxxxxxxxxxx/oauth2/v2.0/token");
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", 
base64EncodedAuthenticationString);
var values = new List<KeyValuePair<string, string>>();
values.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
values.Add(new KeyValuePair<string, string>("scope", 
"https://analysis.windows.net/powerbi/api/.default"));
requestMessage.Content = new FormUrlEncodedContent(values);

HttpResponseMessage response = await client.SendAsync(requestMessage);
var result = await response.Content.ReadAsStringAsync();

Please help!

0

There are 0 best solutions below