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
RayGn2000
Frequent Visitor

Power BI Bearer token REST API throws error The remote server returned an error: (400) Bad Request.

I am trying to refresh PBI dataset programmatically and to do so I am trying to get oath beaer token using the code below. The code works fine with a console app. But when I embed the code into a WCF web service and deploy in a web app then it throws 400) Bad Request error. The reason I had to pass user id and password because the WCF web service will run in backend anf will not have any GUI interaction for user authentication. 

 

resource -> https://analysis.windows.net/powerbi/api

authority -> https://login.microsoftonline.com/*****tenant Id****/oauth2/token

cleint id and ApplicationSecret has been collected from PBI app registration.

 

 

StringBuilder body = new StringBuilder();
            body.Append("resource=" + HttpUtility.UrlEncode(resourceUri));
            body.Append("&client_id=" + HttpUtility.UrlEncode(ApplicationID));
            body.Append("&grant_type=" + HttpUtility.UrlEncode("password"));
            body.Append("&username=" + HttpUtility.UrlEncode(userid));
            body.Append("&password=" + HttpUtility.UrlEncode(password));
            body.Append("&client_secret=" + HttpUtility.UrlEncode(ApplicationSecret));
            
            using (WebClient web = new WebClient())
            {                
                web.Headers.Add("Content-Type", "application/x-www-form-urlencoded");               
                string data = web.UploadString(authority, body.ToString()); // this line throws error
                dynamic result = JsonConvert.DeserializeObject(data);
                try
                {
                    return result.access_token;
                }
                catch
                {

                }
                return null;
            }
5 REPLIES 5
v-shex-msft
Community Support
Community Support

Hi @RayGn2000,

Where are you host the WCF service? Did these host on the same device that you tested the console application? Please share more detailed information to help us clarify your scenario:

How to Get Your Question Answered Quickly 

In addition, please also take a look at the following link about power bi service URLs if you do not configure them on your server white list:

Add Power BI URLs to allow list - Power BI | Microsoft Docs

Regards,
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Hello Xiaoxin  - 

 

I have hosted the WCF service in an Azure web app. The console application works fine in my local computer (VS2019) and I always get a true oauth2 token.

 

When I  access the WCF web service (using web app URL) it throws 400 bad request error.

 

Hi @RayGn2000,

Have you checked the setting of the azure web app configuration to confirm if power bi URLs are in the allow list?

Azure App Service access restrictions - Azure App Service | Microsoft Docs

BTW, you can also contact azure support to confirm if they have limits on the rest API usage when you host on the azure side.
Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Seems the URLs are allowed in the web app.

Hi @RayGn2000,

Did this issue appear when you deploy the code to other web services? I'd like to confirm if these requests have been blocked by security or other azure settings.

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

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.