PowerBIClient: 403 Forbidden error unless preferClientRouting=true is used

Omari Holtz 0 Reputation points
2025-08-08T18:28:02.8333333+00:00

PowerBIClient: 403 Forbidden error unless preferClientRouting=true is used - Request #422710

R. Aaron La Mar

​Omari Holtz​

Description We are experiencing an issue when making API calls with PowerBIClient in C#. In some cases, the request fails with:

 

Operation returned an invalid status code 'Forbidden'

 

We are using Bearer Authentication, and our Application ID is: REDACTED

 

Steps to Reproduce

 

  1. Direct API Call (Fails) GET https://api.powerbi.com/v1.0/myorg/groups Response: 403 Forbidden Operation returned an invalid status code 'Forbidden'

 

  1. API Call with preferClientRouting Parameter (Works) GET https://api.powerbi.com/v1.0/myorg/groups?preferClientRouting=true Response: {   "@odata.context": "https://wabi-us-north-central-g-primary-redirect.analysis.windows.net/v1.0/myorg/$metadata#groups",   "@odata.count": 1,   "value": [     {       "id": " REDACTED ",       "isReadOnly": false,       "isOnDedicatedCapacity": false,       "type": "Workspace",       "name": "REDACTED"     }   ] }

 

Observations Without the preferClientRouting parameter, the API returns 403 Forbidden.

 

With the parameter, the request succeeds.

 

The successful request follows an HTTP 307 Temporary Redirect to a different regional endpoint: Location: https://wabi-us-north-central-g-primary-redirect.analysis.windows.net/v1.0/myorg/groups?preferClientRouting=true It seems that preferClientRouting=true ensures the client correctly follows the redirect, while the direct call does not.

 

Question Is this the expected behavior for Power BI REST API? How can we configure PowerBIClient in C# to handle this redirect automatically without manually appending preferClientRouting=true to every request?

 

Developer technologies | C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Starry Night 30 Reputation points
    2025-08-11T01:57:51.63+00:00

    Please check the official blog to get more information about how to troubleshooting when API call returning 403. For reference: API call returning 403.

    I encountered a known issue with similar problem that when using Export to File REST API, API call returns a 500.  Its workaround is to move the workspace to Premium Gen2.

    0 comments No comments

  2. Adiba Khan 245 Reputation points Microsoft External Staff
    2025-08-11T09:53:23.95+00:00

    Yes it is expected if you are calling the global endpoint instead of your reginal one, because preferClientRouting=true bypasses the redirect step.

    If you want to fix it without manually adding the parameter, you can:

    1.      Point PowerBiClient directly to your reginal base URL(no redirect needed).

    2.      Or customize the HttpClientHandler used by PowerBIClient to preserve the authorization header and follow 307 redirects.

     

    Static async Task Main()

    {              //Your azure AD token for Power BI

                    String accessToken =”<YOUR_ACCESS_TOKEN>”;

                    //The global API endpoint

                    String baseUrl = “https://api.powerbi.com/”;

                   

     

                    //Create HTTP handler that preserves authorization header on redirects

                    Var handler= new AuthRedirectHandler(new HttpClientHandler(), accessToken);

                    Var httpClient = new HttpClient(handler);

     

                    //Create PowerBIClient using your token and custom HTTP handler

                    Var tokenCredentials = new TokenCredentials(accessToken, “Bearer”);

                    Var powerBIClient = new PowerBIClient(new Uri(baseUrl), tokenCredentials, httpClient);

     

                    //Example API call: get all workspaces(groups)

                    Var groups = await powerBIClient.Groups.GetGroupAsync();

                    Foreach(var group in group.Value)

                    {

                                    Console.WriteLine($”Group: {group.Name}”);

                    }

    }

     

     

    What it does:

    ·         Gets your token(accessToken)- needed for authentication with Power BI Rest API

    ·         Wraps HttpClientHandler inside a custom AuthRedirectHandler to follow 307 redirects and keep the bearer token.

    ·         Creates PowerBIClient with the global API URL, Your token credentials and the customer HttpClient for redirect handling.

    ·         Calls GetGroupAsync() to list all PowerBI workspaces your account can access.

    ·         Loops through and prints their names.

     

    Hope this helps. Please try and let me know if any other query

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.