I am struggling for the past few day to query custom logs from Azure Log Analytics. I've been following tutorial provided by Microsoft from https://dev.int.loganalytics.io/documentation/1-Tutorials/Direct-API but I keep getting 403. I granted all permission on the workspace to my Azure Application Here is a simple application code I am using to try to query ALA Workspace
static async Task Main(string[] args)
{
String tenantId = "??????????????????????????????????";
String applicationId = "??????????????????????????????????";";
String applictionSecretKey = "??????????????????????????????????";;
String token;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Dictionary<String, String> requestData = new Dictionary<String, String>();
requestData.Add("grant_type", "client_credentials");
requestData.Add("client_id", applicationId);
requestData.Add("client_secret", applictionSecretKey);
requestData.Add("resource", "https://api.loganalytics.io/");
FormUrlEncodedContent requestBody = new FormUrlEncodedContent(requestData);
var request = await client.PostAsync($"https://login.microsoftonline.com/{tenantId}/oauth2/token", requestBody);
var response = await request.Content.ReadAsStringAsync();
token = JsonConvert.DeserializeObject<dynamic>(response).access_token;
}
String workspaceId = "??????????????????????????????????";;
String query = JsonConvert.SerializeObject(new
{
query = "ApplicationLog_CL | take 10",
timespan = "PT12H"
});
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var postContent = new StringContent(query, Encoding.UTF8, "application/json");
var response = await client.PostAsync($"https://api.loganalytics.io/v1/workspaces/{workspaceId}/query", postContent);
HttpContent responseContent = response.Content;
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);
}
Console.ReadKey();
}
I keep getting 403 response from ALA API. Any clue what I am missing here?