How to generate valid access_token to interact with Business Central - azure

I'm trying to create an automation to interact with Microsoft Business Central. For that I created a Azure App (daemon).
This app has granted permissions from the admin to Read/Write on BC, permissions such as Financials.ReadWrite.All and API.ReadWrite.All and are consented.
When I generate the access token, I can list environment and companies, but cannot list items https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/api-reference/v2.0/api/dynamics_item_get
What other permission do I need?
Here is the error I get when trying to list:
{
"error": {
"code":"Internal_ServerError",
"message":"Sorry, the current permissions prevented the action.(Page APIV2 - Items Execute: _Exclude_APIV2_)"
}
}
If I get the token from here, with my user on it, it works: https://developer.microsoft.com/en-us/graph/graph-explorer

You need to register the Azure App inside Business Central and assign it the required permissions.
You can do this through the Azure Active Directory Applications page.

If you select the app_access permission in you oauth application you also need to give permission inside Business central as Ramon already explained
If you do not assign that permission, and grant consent inside the oauth application already, that app can access all your environments within that tenant

Related

Azure Active Directory Restrict Groups Returning with Graph API

We are trying to get list of users from a particular group in Azure Active Directory.
Steps tried:
Created new app registration
Created client secrets
Gave permissions for Graph API (application permissions) with admin consent.
Then we are getting all the users from all the groups.
Is there any way to restrict the app to only be able to return a specific group's users?
For application permissions, the effective permissions of your app are the full level of privileges implied by the permission. For example, an app that has the User.ReadWrite.All application permission can update the profile of every user in the organization, see here.
You could get the users in particular group with List members API, but the application still has the permissions for all groups.
GET https://graph.microsoft.com/v1.0/groups/{group-id}/members
So far, it's only supported to control mailbox access of an app with ApplicationAccessPolicy.

Why I am able to access the users of a different tenant without adding any API permission to application in Azure Portal?

I created an application named MyApp in my Tenant A with multitenant access. Also, I didn't add any API permissions to it in Azure Portal. I also removed default User.Read permission.
After that, I implement a backend project with using msal4j library. In backend code, I sent harcoded scope Directory.ReadWrite.All.
After that, I run the backend project. The project showed Microsoft sign in pop up in the browser. I provided the credentials of admin of another tenant named B(Tenant B have 16 users). After the successful sign in, the Permission Requested pop-up showed, It was written a description about the Directory.ReadWrite.All. This was normal since I was add Directory.ReadWrite.All as a scope in my backend code.
After approving that requested permission as an admin of Tenant B. I can list the 16 users of the Tenant B with GET /users endpoint of the Microsoft Graph API. So, the MyApp which was created in Tenant A could access the users of Tenant B.
However, how it was possible? Because I didn't add any API permission to my MyApp in the Azure Portal. You can see above screnshot that is empty. I expected to get an error like "Insufficient privilege" when accessing the GET /users endpoint. But I didn't. I can successfully access the all 16 users although I didn't add the Directory.ReadWrite.All API permission in the Azure Portal into MyApp.
If sending Directory.ReadWrite.All as scope from backend project is enough to access to GET /users endpoint. Why we want to use API permissions in the Azure Portal?
The Azure portal permissions are what we call static permissions.
You use them with the ".default" special scope, e.g. https://graph.microsoft.com/.default.
When you specify a scope in the authentication request, that is a dynamic permission.
It is a feature of the newer v2 endpoint that allows you to request the needed permissions at runtime instead of ahead of time.
It's pretty nice for multi-tenant apps since updating permissions can be done more easily, and you can implement optional features better (that require additional permissions).
Docs: https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-permissions-and-consent and https://learn.microsoft.com/en-us/azure/active-directory/develop/consent-framework

How do I add administrator accounts for granting static permissions to my Azure AD App?

I have set up a Azure Active Directory App so that I can access the Microsoft Graph API with MSAL. However, I want to perform API calls without a user (https://learn.microsoft.com/en-us/graph/auth-v2-service) and as such I have added a few permissions that require "Admin consent" to my app. However, I cannot find a way to grant my app these permissions.
I've tried looking around the Azure portal for a way to grant these permissions but without success. I have also tried using the https://login.microsoftonline.com/{tenant}/adminconsent&... link to grant permissions, but unsucessfully so.
The response I received was
AADSTS500201: We are unable to issue tokens from this API version for
a Microsoft account. Please contact the application vendor as they
need to use version 2.0 of the protocol to support this.
I do not have an Azure subscription (not even the free one), but seeing as I was able to add apps to Azure AD as well as get access tokens and then make API calls on behalf of the authorized users I assumed I might not need a subscription.
I just made another app and now I have the grant consent button when I open the API Permissions view.

[Azure AD]: Delete User Group Unauthorized

I was Integration Azure Ad in my application User and Group i was maintain in local that the same scenario i was made in azure using Graph API.
The Create user, create Group, Add the member into Group and remove the member in group also working fine for me. but, the Remove group is not working. it's throw 403 forbidden.
The Sample Post Request is screen is
I was Selected all Application permission.in Microsoft Graph and Active Directory. and Grant Permissions.but, i also Get the Insufficient privileges to complete the operation.
My Access token is
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IkhIQnlLVS0wRHFBcU1aaDZaRlBkMlZXYU90ZyIsImtpZCI6IkhIQnlLVS0wRHFBcU1aaDZaRlBkMlZXYU90ZyJ9.eyJhdWQiOiJodHRwczovL2dyYXBoLndpbmRvd3MubmV0LyIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0LzFlNDVmNzc1LTlhMTAtNDQ1MS04YmI2LTRjNTVkMDgxMDI1Ni8iLCJpYXQiOjE1MDczNzUyNzQsIm5iZiI6MTUwNzM3NTI3NCwiZXhwIjoxNTA3Mzc5MTc0LCJhaW8iOiJZMlZnWU5qNjBKcEZwWkxyNTZFTm9WK2NGejZRQWdBPSIsImFwcGlkIjoiMGJhZTJjMTktMzEwNC00YWM3LTkzNjQtMGQ2NjI1YmU5NDc0IiwiYXBwaWRhY3IiOiIxIiwiaWRwIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvMWU0NWY3NzUtOWExMC00NDUxLThiYjYtNGM1NWQwODEwMjU2LyIsIm9pZCI6ImU5OGRjYTkzLTI2MzktNDU0ZC1hMDg3LWNkZjIzNjg2ODhlMCIsInJvbGVzIjpbIkRldmljZS5SZWFkV3JpdGUuQWxsIiwiRGlyZWN0b3J5LlJlYWQuQWxsIiwiTWVtYmVyLlJlYWQuSGlkZGVuIiwiRGlyZWN0b3J5LlJlYWRXcml0ZS5BbGwiLCJEb21haW4uUmVhZFdyaXRlLkFsbCIsIkFwcGxpY2F0aW9uLlJlYWRXcml0ZS5Pd25lZEJ5IiwiQXBwbGljYXRpb24uUmVhZFdyaXRlLkFsbCJdLCJzdWIiOiJlOThkY2E5My0yNjM5LTQ1NGQtYTA4Ny1jZGYyMzY4Njg4ZTAiLCJ0ZW5hbnRfcmVnaW9uX3Njb3BlIjoiQVMiLCJ0aWQiOiIxZTQ1Zjc3NS05YTEwLTQ0NTEtOGJiNi00YzU1ZDA4MTAyNTYiLCJ1dGkiOiJMVHBWY3VxUFBFMm1uQ1FaYTBrSUFBIiwidmVyIjoiMS4wIn0.liMUK4oOBtNuU-AFHr0VfwFxiIV57pMdha8oae2Xu7tKgQR8HeSTdKm7Ex4s7n7GSAF-2YATXMdFGF5vvfrlF3ALMH8zvENJwY8BOrVsB516z_BROVmagvAKuPWoByRrlDPrhsalJBwwcy_ex1bG-sWvGSBoMpKy53EGWzMt9IF9CSp5IiiVp5xHXHog8BV-P-Bb98smOrnrTK7WhO0ZMkDIUCbfF-4QySwAbFJaUSZPqdZXA8XE-rt7RLZGlpbSpAv5Lmzb2V4mAioO1eSKEjoYQApifCyNHg76FgcRx10PPt1HO2yN9z6s7RaH6H1C1BABJRPaO58SCJI-BsgjFg
Anyone Know Help me.
Thanks in Advance.
Deleting directory objects isn't support at this time using application credentials.
This applies to both the Azure AD Graph and the Microsoft Graph.
The only way to delete objects is using user delegated auth with a token from a user that has sufficient permissions to do so (generally an admin).
PS: I can tell you are using application credentials and application permission because the token you shared, when put in jwt.ms has an appId and roles.

Azure Delegated and Application permission precedence

I've created a sample web application calling a Web API and using oAuth code grant flow against Azure AD.Application is registered in Azure AD and I have given required permission as well through Azure portal. Everything seems working as expected.
There are two option for permission
Delegate Permission
Application Permission
Is it recommended to use mix kind of permission set (App + delegate) for your application?
If I give similar kind of permission to my API on both Delegate and Application,which permission set will take precedence? Will it depend on oAuth flow e.g. Code grant or Implicit ?
In my code how can I differentiate these permission sets while accessing the same resource.I want to call on user context only even same type of application permission is already there?
Application Permissions and Delegated Permissions are completely independent of one another.
Application Permissions apply when you follow the Client Credential Flow (also known as App Only Flow). When you follow this flow, AAD will try to grant permissions to the client application based on the Application Permissions it has predefined in the app registration. These permissions will appear in an App Only Token in the role claim.
In nearly every other flow, where a user is involved (On-Behalf-Of, Authorization Code Grant Flow, Implicit Grant Flow, etc...) AAD will try to grant permissions to the client based on the Delegated Permissions it has predefined. These permissions will appear in App+User tokens in the scp (scope) claim.
You can control the kinds of permissions your app is granted by adjusting the authentication method when getting an access token to a resource.

Resources