I am trying to access graph api using client credentials flow. But getting "Insufficient privileges to complete the operation." error.
All permissions are given to the registered application.
1. AuthenticationContext authenticationContext = new AuthenticationContext(authority);
2. ClientCredential clientCred = new ClientCredential(clientId, clientSecret);
3. AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(graphResource, clientCred).Result;
4. var token = authenticationResult.AccessToken;
5. var delegateAuthProvider = new DelegateAuthenticationProvider((requestMessage) =>
6. {
7. requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token.ToString());
8. return Task.FromResult(0);
9. });
10. // Initializing the GraphServiceClient
11. graphClient = new GraphServiceClient(graphAPIEndpoint, delegateAuthProvider);
12.
13. try
14. {
15.
16. var queryOptions = new List<QueryOption>()
17. { new QueryOption("$count", "true") };
18.
19. var members = graphClient.Groups
20. .Request(queryOptions)
21. .Header("ConsistencyLevel", "eventual")
22. .Top(5)
23. .GetAsync().Result;
24.
25.
26. }
27. catch (Exception ex)
28. {
29.
30. }