I am trying to get an access token from login.microsoftonline.com with azure functions in javascript, any idea how to do it?
Beforehand thank you very much.
Here are few work arounds that you can try
WAY - 1 You can retrieve the access token from postman and then store the same to Azure Key vault and get the secrets in your azure functions.
WAY - 2 Using MSAL for Node.js. The ADAL for node.js package makes node.js apps to connect to AAD and gain access to AAD-protected web pages.
I'm just highlighting the most significant parts here, but the link below will take you to GitHub where you can get the whole website code as well as some information on the library it utilises.
var clientId = 'yourClientIdHere';
var clientSecret = 'yourAADIssuedClientSecretHere'
var authorityHostUrl = 'https://login.windows.net';
var tenant = 'myTenant';
var authorityUrl = authorityHostUrl + '/' + tenant;
var redirectUri = 'http://localhost:3000/getAToken';
var resource = '00000002-0000-0000-c000-000000000000';
var templateAuthzUrl = 'https://login.windows.net/' +
tenant +
'/oauth2/authorize?response_type=code&client_id=' +
clientId +
'&redirect_uri=' +
redirectUri +
'&state=<state>&resource=' +
resource;
authenticationContext.acquireTokenWithAuthorizationCode(
req.query.code,
redirectUri,
resource,
clientId,
clientSecret,
function(err, response) {
var errorMessage = '';
if (err) {
errorMessage = 'error: ' + err.message + '\n';
}
errorMessage += 'response: ' + JSON.stringify(response);
res.send(errorMessage);
}
);
You can refer AAD Library for Node js for more information on this.