Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get azure AD access_token for ServicePrincipal for power bi reports

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?

like image 422
Mahesh Avatar asked Sep 11 '25 04:09

Mahesh


2 Answers

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);
like image 116
unknown Avatar answered Sep 13 '25 19:09

unknown


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;```
like image 27
Jarvis Yang Avatar answered Sep 13 '25 17:09

Jarvis Yang