Reply
Frequent Visitor
Posts: 7
Registered: ‎03-07-2017
Accepted Solution

403 Error when trying to access REST API

Not sure if I'm just missing something. Trying to get to know the REST API, and I can't get past a 403 error.

 

The goal is for the user to click sign in, and then to load a list of their datasets. Sample here:

 

https://dashbi-encore.azurewebsites.net

 

Code:

 

var ADAL = new AuthenticationContext({
    instance: 'https://login.microsoftonline.com/',
    tenant: 'common', 
    clientId: 'ee4d167d-3a52-4578-b2f6-fd6fb8efb84a', 
    redirectUri: 'https://dashbi-encore.azurewebsites.net',
    callback: userSignedIn,
    popUp: true
});

function signIn() {
    ADAL.login();
}

function userSignedIn(err, token) {
    console.log('userSignedIn called');
    if (!err) {

        showWelcomeMessage();

        var trythis = "Bearer " + token;
        var request = new XMLHttpRequest();

        request.open('GET', 'https://api.powerbi.com/v1.0/myorg/datasets');

        request.setRequestHeader('Authorization', trythis);

        request.onreadystatechange = function() {
            if (this.readyState === 4) {
                console.log('Status:', this.status);
                console.log('Body:', this.responseText);
            }
        };

        request.send();
    } else {
        console.error("error: " + err);
    }
}

function showWelcomeMessage() {
    var user = ADAL.getCachedUser();
    var divWelcome = document.getElementById('WelcomeMessage');
    divWelcome.innerHTML = "Welcome " + user.profile.name;
}

I seem to do fine getting the token from AD - and I pass it through in the header of my API request as instructed - but it just won't take. I have correctly created my application and requested the proper permissions (tried via multiple different portals) .


Accepted Solutions
Moderator
Posts: 3,051
Registered: ‎03-06-2016

Re: 403 Error when trying to access REST API


@trevormolag wrote:

Not sure if I'm just missing something. Trying to get to know the REST API, and I can't get past a 403 error.

 

The goal is for the user to click sign in, and then to load a list of their datasets. Sample here:

 

https://dashbi-encore.azurewebsites.net

 

Code:

 

var ADAL = new AuthenticationContext({
    instance: 'https://login.microsoftonline.com/',
    tenant: 'common', 
    clientId: 'ee4d167d-3a52-4578-b2f6-fd6fb8efb84a', 
    redirectUri: 'https://dashbi-encore.azurewebsites.net',
    callback: userSignedIn,
    popUp: true
});

function signIn() {
    ADAL.login();
}

function userSignedIn(err, token) {
    console.log('userSignedIn called');
    if (!err) {

        showWelcomeMessage();

        var trythis = "Bearer " + token;
        var request = new XMLHttpRequest();

        request.open('GET', 'https://api.powerbi.com/v1.0/myorg/datasets');

        request.setRequestHeader('Authorization', trythis);

        request.onreadystatechange = function() {
            if (this.readyState === 4) {
                console.log('Status:', this.status);
                console.log('Body:', this.responseText);
            }
        };

        request.send();
    } else {
        console.error("error: " + err);
    }
}

function showWelcomeMessage() {
    var user = ADAL.getCachedUser();
    var divWelcome = document.getElementById('WelcomeMessage');
    divWelcome.innerHTML = "Welcome " + user.profile.name;
}

I seem to do fine getting the token from AD - and I pass it through in the header of my API request as instructed - but it just won't take. I have correctly created my application and requested the proper permissions (tried via multiple different portals) .


@trevormolag

The 403 error is caused by calling the Power BI get datasets API with an id_token where an acess token is required. Per the ADAL JS lib, to get the access token, you'll have to call ADAL.acquireToken function after login. So you'll need something like

 

 

showWelcomeMessage();

ADAL.acquireToken("https://analysis.windows.net/powerbi/api",function(token){
 
  
		  var trythis = "Bearer " + token;

                  var request = new XMLHttpRequest();

                  request.open('GET', 'https://api.powerbi.com/v1.0/myorg/datasets');

                  request.setRequestHeader('Authorization', trythis);

                  request.onreadystatechange = function () {
                    if (this.readyState === 4) {
                      console.log('Status:', this.status);
                      console.log('Body:', this.responseText);
                    }
                  };

                  request.send();


});

 

Since your question is actually related to Azure AD development, for further questions, I'd suggest you post in the dedicated AAD forum.

 

 

 

View solution in original post


All Replies
Moderator
Posts: 3,051
Registered: ‎03-06-2016

Re: 403 Error when trying to access REST API


@trevormolag wrote:

Not sure if I'm just missing something. Trying to get to know the REST API, and I can't get past a 403 error.

 

The goal is for the user to click sign in, and then to load a list of their datasets. Sample here:

 

https://dashbi-encore.azurewebsites.net

 

Code:

 

var ADAL = new AuthenticationContext({
    instance: 'https://login.microsoftonline.com/',
    tenant: 'common', 
    clientId: 'ee4d167d-3a52-4578-b2f6-fd6fb8efb84a', 
    redirectUri: 'https://dashbi-encore.azurewebsites.net',
    callback: userSignedIn,
    popUp: true
});

function signIn() {
    ADAL.login();
}

function userSignedIn(err, token) {
    console.log('userSignedIn called');
    if (!err) {

        showWelcomeMessage();

        var trythis = "Bearer " + token;
        var request = new XMLHttpRequest();

        request.open('GET', 'https://api.powerbi.com/v1.0/myorg/datasets');

        request.setRequestHeader('Authorization', trythis);

        request.onreadystatechange = function() {
            if (this.readyState === 4) {
                console.log('Status:', this.status);
                console.log('Body:', this.responseText);
            }
        };

        request.send();
    } else {
        console.error("error: " + err);
    }
}

function showWelcomeMessage() {
    var user = ADAL.getCachedUser();
    var divWelcome = document.getElementById('WelcomeMessage');
    divWelcome.innerHTML = "Welcome " + user.profile.name;
}

I seem to do fine getting the token from AD - and I pass it through in the header of my API request as instructed - but it just won't take. I have correctly created my application and requested the proper permissions (tried via multiple different portals) .


@trevormolag

The 403 error is caused by calling the Power BI get datasets API with an id_token where an acess token is required. Per the ADAL JS lib, to get the access token, you'll have to call ADAL.acquireToken function after login. So you'll need something like

 

 

showWelcomeMessage();

ADAL.acquireToken("https://analysis.windows.net/powerbi/api",function(token){
 
  
		  var trythis = "Bearer " + token;

                  var request = new XMLHttpRequest();

                  request.open('GET', 'https://api.powerbi.com/v1.0/myorg/datasets');

                  request.setRequestHeader('Authorization', trythis);

                  request.onreadystatechange = function () {
                    if (this.readyState === 4) {
                      console.log('Status:', this.status);
                      console.log('Body:', this.responseText);
                    }
                  };

                  request.send();


});

 

Since your question is actually related to Azure AD development, for further questions, I'd suggest you post in the dedicated AAD forum.

 

 

 

Highlighted
Regular Visitor
Posts: 33
Registered: ‎10-06-2017

Re: 403 Error when trying to access REST API

@trevormolag, you managed to get this working?

Frequent Visitor
Posts: 7
Registered: ‎03-07-2017

Re: 403 Error when trying to access REST API

[ Edited ]

Yes I did, but not on my own. I ended up using this 

 

https://github.com/Azure-Samples/active-directory-javascript-singlepageapp-dotnet-webapi

 

And modifying it to request Power BI permissions/make Power BI API requests. I suggest anyone else having issues tries out this example before attempting their own implementation. I believe that the issue with my code was as suggested, not calling ADAL.acquireToken.

Regular Visitor
Posts: 33
Registered: ‎10-06-2017

Re: 403 Error when trying to access REST API

@trevormolag, Thanks for the response.

Will you be able to share the sample project for authenticating to powerbi? 

Frequent Visitor
Posts: 7
Registered: ‎03-07-2017

Re: 403 Error when trying to access REST API

[ Edited ]

It's here right now, though I might take it down in the future.

 

https://dashbi-encore.azurewebsites.net

 

Log in, and then visit the "Todo list" tab. Watch the javascript console... you should see a list of your datasets appear.