It seems you use Auth code grant flow to get the access token. The steps of Auth code grant flow is request an authorization code first, and then request for the access token.
First step: Request an authorization code is what you did with the link you provided at the beginning of your question. It will redirect to a url with with "code=xxxxx". You need to get the code
.
Next step: Request for access token with the code
which you got above. Please refer to the screenshot below, the screenshot is what I request for access token with "code". You just need to implement the request below in your function code.
==================================Update===========================
Below is my function code for your reference:
module.exports = async function (context, req) {
var resultcode;
resultcode="0.ARoATqxxxxxxxxxxVtwgAA";
context.res = generatetoken(context,resultcode);
context.res = {
body: "success"
};
}
function generatetoken(context,rescode){
var request = require('request');
var options = {
'method': 'POST',
'url': 'https://login.microsoftonline.com/e4c9xxxxxxxxba2a757fb/oauth2/v2.0/token',
'headers': {
'Content-Type': 'application/x-www-url-form-urlencoded'
},
form: {
'client_id': '7a6f7xxxxxxxxxxfd79e9',
'code': rescode,
'redirect_uri': 'https://hurytest',
'grant_type': 'Authorization_Code',
'scope': 'openid https://graph.microsoft.com/.default',
'client_secret': '2Wjp2xxxxxxxxxxxxXdq4Qckdi'
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
context.res={
body:response
}
});
}
The code above can console the access token success as below screenshot show:
=============================Update 2======================
If you want the token shown in the response of the function, please refer to my code:
module.exports = async function (context, req) {
var resultcode;
resultcode="0.ARoATqvJ5Cxxxxxxxxxxv5wiH9RSQ0gAA";
var result = await generatetoken(context,resultcode);
context.res = {
body: result
};
}
function generatetoken(context,rescode){
var request = require('request');
var options = {
'method': 'POST',
'url': 'https://login.microsoftonline.com/e4c9xxxxxxxxxxxx57fb/oauth2/v2.0/token',
'headers': {
'Content-Type': 'application/x-www-url-form-urlencoded'
},
form: {
'client_id': '7a6fxxxxxxxxxxxxxfd79e9',
'code': rescode,
'redirect_uri': 'https://hurytest',
'grant_type': 'Authorization_Code',
'scope': 'openid https://graph.microsoft.com/.default',
'client_secret': '2WjpxxxxxxxxxxxQckdi'
}
};
return new Promise(function(resolve, reject) {
request(options, function(err, res) {
if (err) {
reject(err);
} else {
resolve(res.body);
}
})
})
}