1

Using C# Dotnet 7 code that calls GraphAPI v5.x, I create a new AppReg in Azure with the necessary information, and this works perfectly. I then try to add a ClientSecret to my new AppReg using the command from the Microsoft site

// Code snippets are only available for the latest version. Current version is 5.x

var graphClient = new GraphServiceClient(requestAdapter);

var requestBody = new Microsoft.Graph.Applications.Item.AddPassword.AddPasswordPostRequestBody
{
    PasswordCredential = new PasswordCredential
    {
        DisplayName = "Password friendly name",
    },
};
var result = await graphClient.Applications["Myapplication-id"].AddPassword.PostAsync(requestBody);

I get the error code : {"Exception of type 'Microsoft.Graph.Models.ODataErrors.ODataError' was thrown."}

I extract the code and message from the catch and obtain the following information:

Error.Code = Request_ResourceNotFound

Error.Message = Resource 'Myapplication-id' does not exist or one of its queried reference-property objects are not present.

As soon as you try to access or modify information using the following code:

  var result = await graphClient.Applications["Myapplication-id"].xxxx => any method

I'm getting the same error. I have repeatedly checked the validity and accuracy of the value of the Myapplication-id, it's a good one.

Has anyone ever solved this problem?

I tried to load the info on my appreg via this

var result = await graphClient.Applications.GetAsync((requestConfiguration) =>
    {
        requestConfiguration.QueryParameters.Filter = "appId eq 'Myapplication-id'";
        requestConfiguration.QueryParameters.Count = true;
        requestConfiguration.QueryParameters.Top = 1;
        requestConfiguration.QueryParameters.Orderby = new string[] { "displayName" };
        requestConfiguration.Headers.Add("ConsistencyLevel", "eventual");
    });

I can see all the properties and add a password like this :

Application requestBody = new()
        {
           PasswordCredentials = new List<PasswordCredential>
            {

                new()
                {
                    DisplayName = "Password friendly name",
                    StartDateTime = DateTimeOffset.Now.AddYears(2),
                    KeyId = Guid.NewGuid()
                }

            }
        };



        var result = await graphClient.Applications["Myapplication-id"].PatchAsync(requestBody);

same result, nothing !

Mlo
  • 21
  • 1

1 Answers1

0

The Graph API documentation declares that you can address the application using either its id or appId but it doesn't work.

GET https://graph.microsoft.com/v1.0/applications/{id}

works, but

GET https://graph.microsoft.com/v1.0/applications/{app_id}

doesn't work. I can confirm that it worked for some time but know it doesn't.

As a workaround you can try to filter your app by appId

var result = await graphClient.Applications.GetAsync((requestConfiguration) =>
{
    requestConfiguration.QueryParameters.Filter = $"appId eq '{appId}'";
    requestConfiguration.QueryParameters.Select = new string []{ "id" };
});

var app = result.Value.FirstOrDefault();

Use Id in the next call

var requestBody = new Microsoft.Graph.Applications.Item.AddPassword.AddPasswordPostRequestBody
{
    PasswordCredential = new PasswordCredential
    {
        DisplayName = "Password friendly name",
    },
};
var result = await graphClient.Applications[app .Id].AddPassword.PostAsync(requestBody);
user2250152
  • 14,658
  • 4
  • 33
  • 57
  • 1
    Top, it works, by testing 100 times you become 'crazy' in the end. In my tests, I also did this, except in the ".Select". Thank you for your reply – Mlo Jun 26 '23 at 06:10