Microsoft Graph API reqquest denied

Simon Bullock 1 Reputation point
2022-11-11T11:19:41.87+00:00

Hello,
I am using the below code to create a GraphServiceClient, which I then try to use with: await client.Groups.Request().Top(999).GetAsync();
which gives me:
Status Code: Forbidden
259449-capture.pngMicrosoft.Graph.ServiceException: Code: Authorization_RequestDenied

private static IAuthenticationProvider CreateAuthorizationProvider(IConfigurationRoot config)
{
var clientId = config["AzureAd:ClientID"];
var clientSecret = config["AzureAd:ClientSecret"];
var redirectUri = config["redirectUri"];
var authority = $"https://login.microsoftonline.com/{config["AzureAd:TenantID"]}/v2.0";

            List<string> scopes = new List<string>();  
            scopes.Add("https://graph.microsoft.com/.default");  
  
            var cca = ConfidentialClientApplicationBuilder.Create(clientId)  
                                                    .WithAuthority(authority)  
                                                    .WithRedirectUri(redirectUri)  
                                                    .WithClientSecret(clientSecret)  
                                                    .Build();  
            return new MsalAuthenticationProvider(cca, scopes.ToArray());  
        }  
        private static GraphServiceClient GetAuthenticatedGraphClient(IConfigurationRoot config)  
        {  
            var authenticationProvider = CreateAuthorizationProvider(config);  
            _graphClient = new GraphServiceClient(authenticationProvider);  
            return _graphClient;  
        }  
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Simon Bullock 1 Reputation point
    2022-11-11T12:08:31.77+00:00

    So the reason was becuase i wasnt setting the permissions in the right application... :(


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.