<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: PowerBI Rest API C# throwing 'Operation returned an invalid status code 'Unauthorized'' in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Rest-API-C-throwing-Operation-returned-an-invalid-status/m-p/3172346#M42075</link>
    <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="bfinnegan7675_0-1680629855943.png" style="width: 400px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/897419iFE489A21C10FD7B9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="bfinnegan7675_0-1680629855943.png" alt="bfinnegan7675_0-1680629855943.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My issue was that the API Permissions needed the Tenant.ReadAll and Tenant.WriteAll permissions in Azure AD.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Antriksh - I did try to change my ExecuteAsync() call to use the .Result but it was not working because it was throwing syntax errors.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 04 Apr 2023 17:38:35 GMT</pubDate>
    <dc:creator>bfinnegan7675</dc:creator>
    <dc:date>2023-04-04T17:38:35Z</dc:date>
    <item>
      <title>PowerBI Rest API C# throwing 'Operation returned an invalid status code 'Unauthorized''</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Rest-API-C-throwing-Operation-returned-an-invalid-status/m-p/3166524#M42040</link>
      <description>&lt;P&gt;Hey all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been following some tutorials and am trying to access some reports from our Power BI instance. I can successfully authenticate and get a Bearer Auth token, but when using my PowerBIClient to try to communicate I get an Unauthorized error.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone tell me if what I am doing is wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help to point me in the right direction would be awesome, thank you!&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;        public async Task&amp;lt;string&amp;gt; GetToken()
        {
            //The client id that Azure AD created when you registered your client app.
            try
            {
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                List&amp;lt;string&amp;gt; scopes = new List&amp;lt;string&amp;gt;();
                scopes.Add("https://analysis.windows.net/powerbi/api/.default");

                var confidentialClient = ConfidentialClientApplicationBuilder
                           .Create("{my app ID from Azure"")
                           .WithClientSecret("{my client secret from azure}")
                           .WithAuthority(new Uri("https://login.microsoftonline.com/{my azure tenant ID}"))
                           .WithRedirectUri("https://localhost:44300/")
                           .Build();

                var accessTokenRequest = confidentialClient.AcquireTokenForClient(scopes);
                var authResult = await accessTokenRequest.ExecuteAsync();

                return authResult.AccessToken;
            }
            catch
            {
                throw;
            }
        }&lt;/LI-CODE&gt;&lt;LI-CODE lang="csharp"&gt;        private async Task Authenticate()
        {
            if (_currentAccessToken == null)
            {
                var tokenCredentials = new TokenCredentials(await GetToken(), "Bearer");

                client = new PowerBIClient(new Uri("https://api.powerbi.com/"), tokenCredentials);

                _currentAccessToken = tokenCredentials;
            }
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public async Task&amp;lt;string&amp;gt; PostExportRequest(Guid reportId, Guid groupId, FileFormat format, IList&amp;lt;string&amp;gt; pageNames = null, string urlFilter = null)
        {
            try
            {
                var exportRequest = new ExportReportRequest
                {
                    Format = format
                };

                if(_currentAccessToken == null) 
                    await Authenticate();
                //also fails
                var groups = await client.Groups.GetGroupsAsync();
                //fails with unauthorized
                var export = await client.Reports.ExportToFileInGroupAsync(reportId, groupId, exportRequest);

                // Save the export ID, you'll need it for polling and getting the exported file
                return export.Id;
            }
            catch
            {
                throw;
            }
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2023 20:10:02 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Rest-API-C-throwing-Operation-returned-an-invalid-status/m-p/3166524#M42040</guid>
      <dc:creator>bfinnegan7675</dc:creator>
      <dc:date>2023-03-31T20:10:02Z</dc:date>
    </item>
    <item>
      <title>Re: PowerBI Rest API C# throwing 'Operation returned an invalid status code 'Unauthorized''</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Rest-API-C-throwing-Operation-returned-an-invalid-status/m-p/3168646#M42047</link>
      <description>&lt;P&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/539883"&gt;@bfinnegan7675&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;var authResult = await accessTokenRequest.ExecuteAsync();

// This should be

var authResult = await accessTokenRequest.ExecuteAsync().Result;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Make sure that the app has access to workspaces, that's the issue I also faced, I created the application but didn't think of giving access to workspaces.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also check if the required API Permissions are granted in Azure Appp Registrations.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2023 07:21:36 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Rest-API-C-throwing-Operation-returned-an-invalid-status/m-p/3168646#M42047</guid>
      <dc:creator>AntrikshSharma</dc:creator>
      <dc:date>2023-04-03T07:21:36Z</dc:date>
    </item>
    <item>
      <title>Re: PowerBI Rest API C# throwing 'Operation returned an invalid status code 'Unauthorized''</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Rest-API-C-throwing-Operation-returned-an-invalid-status/m-p/3172346#M42075</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="bfinnegan7675_0-1680629855943.png" style="width: 400px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/897419iFE489A21C10FD7B9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="bfinnegan7675_0-1680629855943.png" alt="bfinnegan7675_0-1680629855943.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My issue was that the API Permissions needed the Tenant.ReadAll and Tenant.WriteAll permissions in Azure AD.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Antriksh - I did try to change my ExecuteAsync() call to use the .Result but it was not working because it was throwing syntax errors.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 17:38:35 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Rest-API-C-throwing-Operation-returned-an-invalid-status/m-p/3172346#M42075</guid>
      <dc:creator>bfinnegan7675</dc:creator>
      <dc:date>2023-04-04T17:38:35Z</dc:date>
    </item>
    <item>
      <title>Re: PowerBI Rest API C# throwing 'Operation returned an invalid status code 'Unauthorized''</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Rest-API-C-throwing-Operation-returned-an-invalid-status/m-p/3603120#M46486</link>
      <description>&lt;P&gt;but the permission seems too high, need admin consent, is it essential, i meet the same unauthorized errror&lt;/P&gt;</description>
      <pubDate>Wed, 27 Dec 2023 08:16:28 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/PowerBI-Rest-API-C-throwing-Operation-returned-an-invalid-status/m-p/3603120#M46486</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-12-27T08:16:28Z</dc:date>
    </item>
  </channel>
</rss>

