Hi @Manoj Pant
You can try the following sample code to get the application service principal, I have tested it locally and it works fine.
using Azure.Identity;
using Microsoft.Graph;
using Newtonsoft.Json;
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "{tenant id}";
var clientId = "{client id}";
var clientSecret = "{client secret}";
// using Azure.Identity;
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
// https://docs.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
GraphServiceClient graphClient = new GraphServiceClient(clientSecretCredential, scopes);
try
{
var result = await graphClient.ServicePrincipals.GetAsync((requestConfiguration) =>
{
requestConfiguration.QueryParameters.Filter = "appId eq '{app id}'";
requestConfiguration.QueryParameters.Select = new string[] { "appId", "appRoles", "oauth2PermissionScopes", "displayName" };
});
Console.WriteLine(JsonConvert.SerializeObject(result));
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
Also, I don't think you can grant graph application permissions to your app through the graph api, because you can't programmatically grant admin consent for application permissions, currently can only grant admin consent through the UI.
Hope this helps.
If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.