Hello everyone
I have a one box environment trying out the following scenario.
I am creating an extension on Dynamics 365 F&O, whereby I need to get data from an external AAD authenticated azure function app. I am looking for instructions to call this function app from X++ code by passing an access token that grants access to this function app.
The function app reads and reacts to the Office 365 user principal in this way,

So if I am logged in as abc@someuser.com in D365, I want the function app to be invoked in a way such that the userEmail variable above reads the same user email as the one logged in as D365.
Naturally the function app has been configured to have App Service Authentication enabled using a custom Azure AD app that has the following permissions,

My own attempt to generate an access token results in an empty string. I am employing the following X++ code (inspired from BusinessDocumentApiAuthController class).
|
private str generateAccessToken() { IOAuthHelper oAuthHelper = OAuthHelperFactory::GetOAuthHelper(); str resourceId = this.getResourceId(); Sid currentUserSID = this.getCurrentUserSID(); this.createDefaultConfiguration(); str accessToken = oAuthHelper.RetrieveAccessToken(currentUserSID, resourceId);
if (accessToken == "" && Global::hasGUI()) {
str linkFormat = "login.windows.net/%1/oauth2/authorize?redirect_uri=%2&response_type=code&client_id=%3&resource=%4&state=%4"; str link = strFmt("https://" + linkFormat, this.getTenantId(), this.getRedirectURL(), this.getAXClientId(), this.getResourceId()); this.promptUserToClickLink(link); accessToken = oAuthHelper.RetrieveAccessToken(currentUserSID, resourceId); }
return accessToken; }
private void createDefaultConfiguration() { config = new OAuthConfiguration(); config.IsFirstPartyApp = false; //config.AppKey = not set as I would expect user to authenticate using their credential config.ClientId = this.getAXClientId(); config.ResourceId = this.getResourceId(); config.TenantId = this.getTenantId();
IOAuthHelper oAuthHelper = OAuthHelperFactory::GetOAuthHelper(); boolean result = oAuthHelper.UpdateConfiguration(config); }
private str getTenantId() { return '74c2b7d7-ce0c-4486-ba63-1d3535ac4153'; }
private str getResourceId() { return System.Web.HttpUtility::UrlEncode('fictitiousFunctionApp.azurewebsites.net'); }
public str getAXClientId() { IApplicationEnvironment environment = EnvironmentFactory::GetApplicationEnvironment(); IAzureActiveDirectoryConfig aadConfig = environment.Aad; str realm = aadConfig.Realm;
if (strStartsWith(realm, "spn:")) { realm = subStr(realm, 5, strLen(realm) - 4); } return realm; }
public str getRedirectURL() { IApplicationEnvironment environment = EnvironmentFactory::GetApplicationEnvironment(); IInfrastructureConfig infrastructureConfig = environment.Infrastructure;
return System.Web.HttpUtility::UrlEncode(strFmt('%1/oauth', infrastructureConfig.HostUrl.TrimEnd('/'))); }
|
When I click the link that opens in a new window, I get this error in my URL,
The client has requested access to a resource which is not listed in the requested permissions in the client’s application registration.