Search code examples
javascriptazure-active-directorypowerbipowerbi-embeddedservice-principal

Migrating master user to service principal for authentication on power bi embedded


I'm trying to migrating my authentication method from Power BI Master User to service principal.

on master user I'm using msal with authentication flow like bellow: login to AAD --> request for AAD token --> importing pbix file with rest API using AAD token as credential

this is the code

$(document).ready(function () {
    myMSALObj.loginPopup(requestObj).then(function (loginResponse) {
        acquireTokenPopup();
    });
    Msal.UserAgentApplication
});

function acquireTokenPopup() {
    myMSALObj.acquireTokenSilent(requestObj).then(function (tokenResponse) {
        AADToken = tokenResponse.accessToken;
        importPBIX(AADToken);
    });
}

function importPBIX(accessToken) {
    xmlHttp.open("GET", "./importPBIX?accessToken=" + accessToken + "&pbixTemplate=" + pbixTemplate, true);
    //the rest of import process//
}

so there are two question: 1. what kind of flow would it be if I use service principal instead? on my head and from the info which I read from microsoft document it would be simpler like: request token using application secret key --> importing pbix file with rest API using token is this correct? 2. what kind of code that I can use to do this on javascript?I think MSAL couldn't do token request by using service principal. would appreciate any info or tutorial for this.

bests,


Solution

  • thanks to Jim's answer, I've tweaked my code a little bit and the token authentication process went smoothly. As my apps using javascript at front-end and python as its back-end, I decided to do the process at python and used python msal library instead. the code is just like :

    authority_host_uri = 'https://login.microsoftonline.com'
    tenant = 'myTenantId'
    authority_uri = authority_host_uri + '/' + tenant
    client_id = 'myClienId'
    client_secret = 'myClientSecretKey'
    config={"scope":["https://analysis.windows.net/powerbi/api/.default"]}
    
    app = ConfidentialClientApplication(client_id, client_credential=client_secret, authority=authority_uri)
    token = app.acquire_token_for_client(scopes=config['scope'])
    

    once again thanks to Jim for helping me on this one.

    bests,