I am trying to access MS Graph using an AAD account, this account is global admin and has every right delegated. I want to do it without interactive sign-in, i.e using UserPasswordCredential. When trying to access MS Graph I get the error:
My flow:
Getting token:
public async Task<string> GetUserAccessTokenAsync()
{
UserPasswordCredential userPasswordCredential = new UserPasswordCredential("[email protected]", "password");
AuthenticationContext authContext = new AuthenticationContext("https://login.microsoftonline.com/tenant.onmicrosoft.com");
AuthenticationResult token = await authContext.AcquireTokenAsync("https://graph.windows.net", appId, userPasswordCredential);
return token.AccessToken;
}
using token:
public static GraphServiceClient GetAuthenticatedClient()
{
GraphServiceClient graphClient = new GraphServiceClient(
new DelegateAuthenticationProvider(
async (requestMessage) =>
{
Adal adal = new Adal();
string accessToken = await adal.GetUserAccessTokenAsync();
// Append the access token to the request.
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
}));
return graphClient;
}
Trying to call MS Graph to read events:
try
{
// Get events.
items = await eventsService.GetMyEvents(graphClient);
}
catch (ServiceException se)
{
//this is where I get the error
}
Any ideas where I'm going wrong?

