Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

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-core#client-id, 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

 

 

 

  • Anonymous's avatar
    Anonymous
    5 years ago

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

6 Replies