Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
simonagren
Regular Visitor

JavaScript call API

I've tried to understand how everything works, registering apps etc. Autheticating with ADAL etc.

If anyone knows step by step, or have a sample where some JavaScript works, it woudl be helpful

 

I am trying to build an angular 2 app (but it can be pure javascript as well) to, embed on a webpage.

From that app I want to call  https://api.powerbi.com/v1.0/myorg/dashboards and auth with a token.

 

So since the app will be hosted at http://adress.com/somePage. Should I register an app with PowerBI, or not? Since I seem to need the clientId to get a token.

 

In this scenario I have logged in as myself and getting the cached token, to send to the API. 

 

This is some code I've tried, just trying to call it from localhost, but I get a 403:

    let token = this.adalService.getCachedToken(this.adalService.config.clientId);
    let headers = new Headers({
        'Authorization': 'Bearer ' + token,
        'Accept': 'application/json',
        'Content-Type': 'application/json; charset=utf-8',
    });
    let options = new RequestOptions({ headers: headers });


    return this.http.get(`https://api.powerbi.com/v1.0/myorg/dashboards`, options)
        .toPromise()
        .then((res) => { return res; })
        .catch((e) => { console.error(e); });

 

 

2 REPLIES 2
Eric_Zhang
Employee
Employee


@simonagren wrote:

I've tried to understand how everything works, registering apps etc. Autheticating with ADAL etc.

If anyone knows step by step, or have a sample where some JavaScript works, it woudl be helpful

 

I am trying to build an angular 2 app (but it can be pure javascript as well) to, embed on a webpage.

From that app I want to call  https://api.powerbi.com/v1.0/myorg/dashboards and auth with a token.

 

So since the app will be hosted at http://adress.com/somePage. Should I register an app with PowerBI, or not? Since I seem to need the clientId to get a token.

 


@simonagren

Have you debugged the token value and try to call the dashboard API in some other API test console like postman?

 

As 403 indicates an unauthorization problem, so your question seems actually related to AAD authentication, I'd suggest you post it in the dedicated AAD forum.  You'd get more proper response there.

 

You can also check this github lib azure-activedirectory-library-for-js.

 

After getting a valid token, we can help to call the Power REST APIs.

simonagren
Regular Visitor

I've tried to understand how everything works, registering apps etc. Autheticating with ADAL etc.

If anyone knows step by step, or have a sample where some JavaScript works, it woudl be helpful

 

I am trying to build an angular 2 app (but it can be pure javascript as well) to, embed on a webpage.

From that app I want to call  https://api.powerbi.com/v1.0/myorg/dashboards and auth with a token.

 

So since the app will be hosted at http://adress.com/somePage. Should I register an app with PowerBI, or not? Since I seem to need the clientId to get a token.

 

In this scenario I have logged in as myself and getting the cached token, to send to the API. 

 

This is some code I've tried, just trying to call it from localhost, but I get a 403:

 let token = this.adalService.getCachedToken(this.adalService.config.clientId);
    let headers = new Headers({
        'Authorization': 'Bearer ' + token,
        'Accept': 'application/json',
        'Content-Type': 'application/json; charset=utf-8',
    });
    let options = new RequestOptions({ headers: headers });


    return this.http.get(`https://api.powerbi.com/v1.0/myorg/dashboards`, options)
        .toPromise()
        .then((res) => { return res; })
        .catch((e) => { console.error(e); });

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.