I'm quite new to Azure Functions. I'm trying to consume Graph APIs inside an HTTP triggered Azure Function in javascript. I found few docs related to this argument. Also a couple of questions here on Stack Overflow, but all the material is quite old and both Graph and Azure Functions have improved from then. When I just create the function, it work perfectly. If I simple add a binding to Auth Token (https://graph.microsoft.com), without even touching the index.js, the function stops working and returns 404 error (file not found). The connection try is not even logged in monitor or Applications Insights. I'm building the function directly from the Azure portal (no local deployment). I already created an app on AAD and changed the authentication on my function app to connect to this app. Also authorized the Azure user on Microsoft Graph (through Graph Explorer), but still get this 404 error. What should I check ?
Edit based on Tony Ju answer:
I turned off App Service Authentication and followed the steps here, as suggested. I just changed few things to make it work in Azure Function (the post was for pure Node.js). actually, this is my index.js:
const APP_ID = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX';
const APP_SECERET = 'XXXX~-XXX~XXXXXXXXXXXXX~XXXXXXXXX~';
const TOKEN_ENDPOINT ='https://login.microsoftonline.com/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/oauth2/v2.0/token';
const MS_GRAPH_SCOPE = 'https://graph.microsoft.com/.default';
const axios = require('axios');
const qs = require('qs');
const postData = {
client_id: APP_ID,
scope: MS_GRAPH_SCOPE,
client_secret: APP_SECERET,
grant_type: 'client_credentials'
};
module.exports = function (context, req) {
axios.defaults.headers.post['Content-Type'] =
'application/x-www-form-urlencoded';
let token = '';
axios
.post(TOKEN_ENDPOINT, qs.stringify(postData))
.then(response => {
context.log(response.data);
})
.catch(error => {
context.log(error);
});
};
the XXXX are taken from AAD app, repectively: Application (client) ID, App Client secret and Directory (tenant) ID.
My function.json is the following:
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"name": "graphToken",
"direction": "in",
"type": "token",
"resource": "https://graph.microsoft.com",
"identity": "userFromRequest"
}
]
}
Still get 404 error. Doesn't seem something related to code, but something related to function bindings.
Edit after new comment from Tony Ju and further experiments:
Thanks to Tony: auth binding is not necessary.
Also need to go to Function App Service, then to Console (under Development Tools) and issue
npm install axios
npm install qs
Also, in my previous listing for index.js, function need to be async:
module.exports = async function (context, req) {
In this way, it work. Thanks Tony.