Search code examples
azureazure-web-app-serviceaccess-tokenazure-ad-graph-apiazure-webapps

Getting "an error occurred while sending the request" while trying to create an application registration on Azure programmatically


I have a use case for a project where I need to automate the creation of Application Registration on Azure programmatically.

I have been using this sample. for doing so.

I am also following the documentation from microsoft on creating an application.

enter image description here

I have done the same in my code snippet as shown in the image below and also added the functionality to add a password to the created Application.

enter image description here

This process used to work before where in the Application used to get registered and the flow was as expected.

However now when I try running this request, I get an error saying "Error occurred while sending the request". There does not seem to be any documentation related to error codes related to this.

Can you help me find the relevant material or changes that have been made my microsoft on graph client that may be causing this?

Update

As per comments I found the IDs of the needed API Permissions using Powershell. I added them to my code as well as follows

enter image description here

The Permissions added are Application.ReadWrite.All and Application.ReadWrite.OwnedBy respectively as Type role. I still get the same error.

The inner exception is as follows:

enter image description here


Solution

  • Summarize from the comments:

    The problem was caused by permissions. We should add the required permissions(add at least one of the two permissions Application.ReadWrite.All, Directory.AccessAsUser.All in Delegated type) to the registered app "API permissions" tab and then just use https://graph.microsoft.com/.default as scope in code.

    By the way, if want to add the permissions by their id, we can get the permissions and ids by request this url: https://graph.microsoft.com/v1.0/serviceprincipals?$filter=appId eq '00000003-0000-0000-c000-000000000000'

    ================================Update===========================

    First you need to have a registered app in your Azure AD. Here I have an app named "huryGetToken4". If you do not have the app, you can refer to this document to register the app. Go to your registered app and click "API permissions" tab. Add the permissions which are required. enter image description here

    enter image description here

    Please do not forget do "Grand admin consent for xxx" after add the permissions.

    Then click "Certificates & secrets" tab and new a client secret. Copy the secret to your notepad. enter image description here

    Then we can develop the code, below is my code for your reference:

    using Microsoft.Graph;
    using Microsoft.Graph.Auth;
    using Microsoft.Identity.Client;
    using System;
    
    namespace ConsoleApp7
    {
        class Program
        {
            static async System.Threading.Tasks.Task Main(string[] args)
            {
                Console.WriteLine("Hello World!");
    
                IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
                .Create("clientId")
                .WithTenantId("tenantId")
                .WithClientSecret("clientSecret")
                .Build();
    
                ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
    
                GraphServiceClient graphClient = new GraphServiceClient(authProvider);
    
                var application = new Application
                {
                    DisplayName = "huryNewApp1"
                };
    
                await graphClient.Applications.Request().AddAsync(application);
            }
        }
    }
    

    For the clientId and tenantId in my code, you can find them on the "Overview" page of your registered app. enter image description here

    After running the code, the new app will be created in Azure AD.

    enter image description here