Reputation: 852
I am trying to create a sample NodeJS App where I will be calling Azure Log Analytics REST API and I have App Registration already done but I am not a Node JS developer and unfortunately stuck here, I got some code from some searching around but this doesnt work at all, can someone help me how I can get this to get Authentication token from NodeJs (I just created a sample Express NodeJs app and pasted the below code) but its not working, if someone can help me with fixing the code:
var express = require('express');
const axios = require('axios');
const qs = require('qs');
const APP_ID = 'XXXXXXXXXXXXXXXXXXX';
const APP_SECERET = 'YYYYYYYYYYYYYYYYY';
const TOKEN_ENDPOINT ='https://login.microsoftonline.com/MYTENANTID/oauth2/v2.0/token';
const MS_GRAPH_SCOPE = 'Data.Read';
const resource ='GUID FOR LOG ANALYTICS WORKSPACE';
var responseval = "";
const postData = {
client_id: APP_ID,
scope: MS_GRAPH_SCOPE,
client_secret: APP_SECERET,
grant_type: 'client_credentials',
resource: resource
};
axios.defaults.headers.post['Content-Type'] =
'application/x-www-form-urlencoded';
let token = '';
axios
.post(TOKEN_ENDPOINT, qs.stringify(postData))
.then(response => {
console.log(response.data);
responseval = response.data;
})
.catch(error => {
console.log(error);
responseval = error;
});
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express ' + responseval });
});
module.exports = router;
Following is my WORKING POWERSHELL CODE which I am trying to get working in NODE.Js
$tenantId = "MYTENANT ID" #Directory ID for THREE PROJECT
$formData = @{
client_id = "XXXXXXXXXXXXXX";
client_secret = "YYYYYYYYYYYYYYYYYYYY";
scope = 'Data.Read';
grant_type = 'client_credentials';
resource = 'https://api.loganalytics.io'
}
$uri = 'https://login.microsoftonline.com/' + $tenantId + '/oauth2/token?api-version=1.0'
$response = Invoke-RestMethod -Uri $uri -Method Post -Body $formData -ContentType "application/x-www-form-urlencoded"
$authHeader = @{
'Content-Type'='application/json'
'Authorization'= 'Bearer ' + $response.access_token
}
$request1 = "https://api.loganalytics.io/v1/workspaces/LOGANALYTICS WORKSPACE ID/query?query=externalapistatus_CL "
$resultz1 = Invoke-RestMethod -Uri $request1 `
-Headers $authHeader `
-Method Get
Upvotes: 0
Views: 478
Reputation: 852
After some little basics knowledge gathering Thanks to Node.JsTutorial by Programming with @Mosh (Youtube Node.Js basics tutorial) I was able to get this working. I built a simple console Node.Js app as suggested by @Gary above and following is the code which returns the token now (Next step now will be to put it in function and use it in web-App):-
const axios = require('axios');
const oauth = require('axios-oauth-client');
const qs = require('qs');
const APP_ID = 'XXXXXXXXXXXXXXXX';
const APP_SECERET = 'YYYYYYYYYYYYYYYY';
const TOKEN_ENDPOINT ='https://login.microsoftonline.com/MyTenantID/oauth2/token?api-version=1.0';
const MS_GRAPH_SCOPE = 'Data.Read';
const resource ='https://api.loganalytics.io';
var responseval = "";
const postData = {
client_id: APP_ID,
scope: resource,
client_secret: APP_SECERET,
grant_type: 'client_credentials'
};
axios.defaults.headers.post['Content-Type'] =
'application/x-www-form-urlencoded';
axios
.post(TOKEN_ENDPOINT, qs.stringify(postData))
.then(function(response){
console.log(response);
})
.catch(function (err){
console.log(err.response);
});
Upvotes: 0
Reputation: 29218
You are just writing an http console client so you don't need express. Maybe use this library.
For the simplest code use a self executing function like this in an index.js file - then just run 'npm start'
const axios = require('axios');
const oauth = require('axios-oauth-client');
(async () => {
const getClientCredentials = oauth.client(axios.create(), {
url: 'https://oauth.com/2.0/token',
grant_type: 'client_credentials',
client_id: 'foo',
client_secret: 'bar',
scope: 'baz'
});
const auth = await getClientCredentials(); // => { "access_token": "...", "expires_in": 900, ... }
})();
Upvotes: 0