0
votes

I have .NET Core console application and I'm trying to acquire access token for SharePoint API using MSAL.NET library.

In Azure I defined a new permission for my app. enter image description here

In code I'm acquiring acces token

var clientApp = PublicClientApplicationBuilder.Create("<client_id>")
                .WithAuthority($"https://login.microsoftonline.com/{<tenant_id>}")
                .WithDefaultRedirectUri()
                .Build();
var accounts = await clientApp.GetAccountsAsync();
var firstAccount = accounts.FirstOrDefault();
var authResult = await clientApp.AcquireTokenSilent("https://tenant.sharepoint.com/.default", firstAccount).ExecuteAsync();

AcquireTokenSilent throws MsalUiRequiredException, so I call

authResult = await clientApp.AcquireTokenByUsernamePassword(new string[] { "https://tenant.sharepoint.com/.default" }, "userName", <password>).ExecuteAsync();

but it returns

The remote server returned an error: (401) Unauthorized.

According to MSAL, it's possible to use user's credentials for acquiring access token.

If I send a simple POST request based on v2-oauth-ropc I receive the access token. I suppose that MSAL.NET internally sends the same POST request.

1

1 Answers

0
votes

Code should work. Please provide a request or correlation id and timestamp.