I need to get Azure AD Access token from Vanilla Javascript code to use it for Power BI reports. I could get some working samples in .net from https://github.com/microsoft/PowerBI-Developer-Samples. But as the website that am using built on third part wordpress like CMS I can't use it.
All i need is to get Azure AD access token from vanilla javascript or either some post man request?
I have below configuration values for my power BI report,
"AzureAd": { "AuthenticationMode": "ServicePrincipal", "AuthorityUri": "https://login.microsoftonline.com/organizations/", "ClientId": "XXXXXXXXX", "TenantId": "XXXXXXXXXXX", "Scope": [ "https://analysis.windows.net/powerbi/api/.default" ], "PbiUsername": "", "PbiPassword": "", "ClientSecret": "XXXXXXXXXX" }, "PowerBI": { "WorkspaceId": "XXXXXXXXXXX", "ReportId": "XXXXXXXXXXX" },
I have tried few javascript samples from below github repo but they did not work, https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-browser.
Any idea how to get it?
It sounds like you would like to get Azure AD access token for PowerBI with client credentials flow(app-only). You could use @azure/msal-node
in javascript, sample here.
const msal = require('@azure/msal-node');
// Initialize MSAL
const msalConfig = {
auth: {
clientId: "Enter_the_Application_Id_Here",
authority: "https://login.microsoftonline.com/Enter_the_Tenant_Id_Here",
clientSecret: "Enter_the_Client_Secret_Here",
}
};
const cca = new msal.ConfidentialClientApplication(msalConfig);
// Requesting tokens
const tokenRequest = {
scopes: [ 'https://analysis.windows.net/powerbi/api/.default' ],
};
const tokenResponse = await cca.acquireTokenByClientCredential(tokenRequest);
This is what I used for my auth.js file:
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
// ----------------------------------------------------------------------------
const getAccessToken = async function () {
try {
const msal = require('@azure/msal-node');
// Create a config variable that store credentials from config.json
// replace with
let config = require("../config/config.json");
let authorityUri = 'https://login.microsoftonline.com/' + `${config.tenantId}`
// Initialize MSAL
const msalConfig = {
auth: {
clientId: `${config.clientId}`,
authority: `${authorityUri}`,
clientSecret: `${config.clientSecret}`,
}
};
const cca = new msal.ConfidentialClientApplication(msalConfig);
// Requesting tokens
const tokenRequest = {
scopes: ['https://analysis.windows.net/powerbi/api/.default'],
};
// async function tokenResponse() {
let response = await cca.acquireTokenByClientCredential(tokenRequest);
return response;
} catch (error) {
console.log(error);
}
}
module.exports.getAccessToken = getAccessToken;```
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With