I am developing a daemon app that interacts with Microsoft 365 Office Planner to manipulate Microsoft Graph tasks. When I call the Microsoft Graph API to get the tasks related to my tenant, I get an Unauthorized request exception.
I have registered my application in Azure Active Directory and also gave it permissions to use the Microsoft Graph.
I request an access token based on the process from here: https://graph.microsoft.io/en-us/docs/authorization/app_only
I am able to get a token from the Azure Active Directory v2.0 endpoint. The request code is the following:
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_id", "<clent id>"),
new KeyValuePair<string, string>("client_secret", "<client secret>"),
new KeyValuePair<string, string>("resource", @"https://graph.microsoft.com")
var content = new FormUrlEncodedContent(pairs);
var response = client.PostAsync("https://login.microsoftonline.com/<tenant id>/oauth2/token", content).Result;
When I use this access token to perform a request as follow:
client.DefaultRequestHeaders.Authorization= new AuthenticationHeaderValue("Bearer", token);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
var response = client.GetAsync(@"https://graph.microsoft.com/beta/tasks").Result;
I get a status code 401 Unauthorized with the following response message:
Unauthorized: Access is denied due to invalid credentials.
You do not have permission to view this directory or page using the credentials that you supplied.
Is there any authorization process I have not performed to grant access to my app. Please help!!!!
Thanks in advance!!