How to get PowerBI accesstoken using ADAL.JS

2019-08-07 03:36发布

问题:

I'm trying to use ADAL.js to authenticate against PowerBI in order to get an access_token and the embed_token needed to embed PowerBI reports/dashboards/tiles in a html/javascript-only "webpart". My adal-config looks like:

config = {
    instance: 'https://login.windows.net/common/oauth2/authorize/',
    tenant: 'tenant.onmicrosoft.com',
    clientId: '05xxxxx-xxx-xxxx-xxxx-xxxxxxxxxxxx',
    loginResource: "https://analysis.windows.net/powerbi/api",
    postLogoutRedirectUri: window.location.origin,
    cacheLocation: 'localStorage', 
};

But I can't seem to find any access-token etc in the user.profile I get. I am obviously missing something but what.. :) Any help would be much appriciated

回答1:

Looking at: https://community.powerbi.com/t5/Developer/get-Access-token-using-js/m-p/350294 and also this: https://community.powerbi.com/t5/Developer/How-to-Generate-Embed-Token-in-pure-JavaScript/td-p/350056

you can use ADAL.js to get the access token itself

window.config  = {
  instance: 'https://login.microsoftonline.com/',
  tenant: 'common', //COMMON OR YOUR TENANT ID

  clientId: 'XXXXX', //This is your client ID
  redirectUri: 'XXXXXX', //This is your redirect URI

  callback: userSignedIn,
  popUp: true
};

var ADAL = new AuthenticationContext(config);

function signIn() {
  ADAL.login();
}
				
function userSignedIn(err, token) {
  console.log('userSignedIn called');
  if (!err) {
    showWelcomeMessage();
    ADAL.acquireToken("https://analysis.windows.net/powerbi/api", function (error, token) {
      // Handle ADAL Error
      if (error || !token) {
      printErrorMessage('ADAL Error Occurred: ' + error);
      return;
    }
}