I need to fetch the API token first and then call the API. How can I achieve this result.
To fetch the access token for an API, you have to create an Azure AD Application and Expose an API like below:

And add the API permissions like below:

Now, I generated auth-code using below authorize endpoint:
https://login.microsoftonline.com/TenantID/oauth2/v2.0/authorize?
&client_id=ClientID
&response_type=code
&redirect_uri=https://jwt.ms
&response_mode=query
&scope=api://APIAppClientID/access_as_user
&state=12345

I generated access token via Postman by using below parameters:
https://login.microsoftonline.com/TenantID/oauth2/v2.0/token
client_id:ClientID
grant_type:authorization_code
scope:api://APIAppClientID/access_as_user
code:code
redirect_uri:https://jwt.ms
client_secret:ClientSecret
The access token got generated for the API like below:

By using the above access token, you can now call the API.
To get the access token using @azure/msal-angular
, make use of below code refer this Blog.
@NgModule({
imports: [
MsalModule.forRoot({
auth: {
clientId: "Your client ID"
}
}, {
protectedResourceMap: [
['https://api.myapplication.com/users/*', ['access_as_user']]
]
})
],
providers: [
ProductService,
{
provide: HTTP_INTERCEPTORS,
useClass: MsalInterceptor,
multi: true
}
]
})
export class AppModule {}
Reference:
microsoft-authentication-library-for-js/lib/msal-angular AzureAD/microsoft-authentication-library-for-js · GitHub by tnorling