0
votes

I am trying to access office 365 users using Graph SDK provided by Microsoft in NuGet my code is :

var cca = ConfidentialClientApplicationBuilder
                .Create("ClientId")
                .WithClientSecret("ClientSecret")
                .WithTenantId("TenantId")
                .Build();

List<string> scopess = new List<string>();
scopes.Add("https://graph.microsoft.com/.default");
scopes.Add("Calendars.Read");
// i tried each scope alone and same result
var authenticationProvider = new AuthorizationCodeProvider(cca, scopes.ToArray());
GraphServiceClient graphClient = new GraphServiceClient(authenticationProvider);
var x = await graphClient.Users.Request().GetAsync();

on the last line i am getting the error

Authentication challenge is required

my Azure Active Directory app registration settings are : enter image description here enter image description here enter image description here

probably i am doing something wrong but not sure what! I am using the Microsoft Docs as a reference

1

1 Answers

2
votes

You are using client credential provider.

Replace var authenticationProvider = new AuthorizationCodeProvider(cca, scopes.ToArray()); with ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(cca);

And note that you define GraphServiceClient graphClient but you are using graphClient2 in the next line: graphClient2.Users.Request().GetAsync(). Please Check it.