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
Anonymous
Not applicable

Using acquireTokenWithClientCredentials, got token, but failed to use REST API - error "Unauthorized

I am following the Power BI embed guide: https://docs.microsoft.com/en-us/power-bi/developer/embedded/embed-sample-for-customers?tabs=net-cor..., using “Service Principle” for authentication.  I see issues already:

 

Validation #1:

I download a nodejs sample code that is used to validate the embed. 

 } else if (config.authenticationMode.toLowerCase() === "serviceprincipal") {

        authorityUrl = authorityUrl.replace("common", config.tenantId);

        let context = new AuthenticationContext(authorityUrl);

        return new Promise(

            (resolve, reject) => {

                context.acquireTokenWithClientCredentials(

                    config.scope, 

                    config.clientId, 

                    config.clientSecret, 

                    function (err, tokenResponse) {

 

                    // Function returns error object in tokenResponse

                    // Invalid Username will return empty tokenResponse, thus err is used

                    if (err) {

                        reject(tokenResponse == null ? err : tokenResponse);

                    }

                    console.log(tokenResponse);

                    resolve(tokenResponse);

                })

            }

        );

    }

 

So far, I can see the “access token” is back, but while using that access token with Power BI REST API - Get Reports In Group, then I see an Error – Unauthorized.

 

Validation #2

If I test the REST API using: https://docs.microsoft.com/en-us/rest/api/power-bi/reports/getreportsingroup , then I can see the response coming back correctly.

 

Then, I noticed that the access token is different in term of what I get from nodejs code vs. the one in https://docs.microsoft.com/en-us/rest/api/power-bi/reports/getreportsingroup

 

So I hard code the access token from Validation #2 into the nodejs code, then I can see the report I created in Power BI in testing web page, which is what I am looking for. 

 

Now the question is why the access token I got from nodejs api is not working?  Did I miss something?  😊

 

Any ideas?

 

Thanks,

Yong

 

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

It 's confiruration issue.  I did have the admin rights.  Asked IT admin to help out.

View solution in original post

6 REPLIES 6
V-lianl-msft
Community Support
Community Support

Hi @Anonymous ,

 

Please refer to these threads:

https://community.powerbi.com/t5/Developer/Generating-Power-Bi-Access-Token-in-NodeJS/td-p/276522 

https://community.powerbi.com/t5/Developer/API-Generate-Embed-Token-in-Node-js/td-p/405201 

 

Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Anonymous
Not applicable

Thanks for the info.  But the issue I have is different.  We are not generating Access Token, we acquire the Access Token using Azure AD secret ... the issue is why the token returned does not work when it is used with Power BI REST API ?

 

Thanks

Yong

Anonymous
Not applicable

It 's confiruration issue.  I did have the admin rights.  Asked IT admin to help out.

Do you mean you did NOT have admin rights? I'm also stuck on this. 

Anonymous
Not applicable

Yes, did NOT have the admin rights.  

Thank you for the clarification, but I'm still a bit confused. I am an admin im my workspace, but I'm still getting this error. It seems to me, that once you get the Token for the clientId/Secret, requests are made under that context, so my own credentials (and whether or not I'm an admin) shouldn't matter. 

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.