<?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: REST Api  - Access Token 'The remote server returned an error: (401) Unauthorized.' in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/REST-Api-Access-Token-The-remote-server-returned-an-error-401/m-p/1360565#M25653</link>
    <description>&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/256993"&gt;@MrRichDean&lt;/a&gt;,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;It seems like the new version NuGet pack changes the defined so that the old version code not working. Please try to use the following code replaces the raw part if it works on your side:&lt;/FONT&gt;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;private static string GetToken()
        {
            // and add using Microsoft.IdentityModel.Clients.ActiveDirectory

            //The client id that Azure AD created when you registered your client app.
            string clientID = "{Client_ID}";

            //RedirectUri you used when you register your app.
            //For a client app, a redirect uri gives Azure AD more details on the application that it will authenticate.
            // You can use this redirect uri for your client app
            string redirectUri = "https://login.live.com/oauth20_desktop.srf";

            //Resource Uri for Power BI API
            string resourceUri = "https://analysis.windows.net/powerbi/api";

            //OAuth2 authority Uri
            string authorityUri = "https://login.microsoftonline.com/common/";

            //Get access token:
            // To call a Power BI REST operation, create an instance of AuthenticationContext and call AcquireToken
            // AuthenticationContext is part of the Active Directory Authentication Library NuGet package
            // To install the Active Directory Authentication Library NuGet package in Visual Studio,
            //  run "Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory" from the nuget Package Manager Console.

            // AcquireToken will acquire an Azure access token
            // Call AcquireToken to get an Azure token from Azure Active Directory token issuance endpoint
            AuthenticationContext authContext = new AuthenticationContext(authorityUri);

            string token = authContext.AcquireTokenAsync(resourceUri, clientID, new Uri(redirectUri), (IPlatformParameters)new UserCredential()).Result.AccessToken;

            Console.WriteLine(token);
            Console.ReadLine();

            return token;
        }&lt;/LI-CODE&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Regards,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Xiaoxin Sheng&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 10 Sep 2020 07:21:28 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2020-09-10T07:21:28Z</dc:date>
    <item>
      <title>REST Api  - Access Token 'The remote server returned an error: (401) Unauthorized.'</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/REST-Api-Access-Token-The-remote-server-returned-an-error-401/m-p/1346664#M25546</link>
      <description>&lt;P&gt;Good morning / afternoon / evening everyone&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First of all, apologies for my ignorance and for any stupid questions I ask. I'm new to Power BI so very much finding my feet.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ok, so what I am trying to do is creat a small console app that gets the refresh history for a particular dataset but I am having all sorts of trouble with the access token. Here is the code I have:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;        private static string GetToken()
        {
            //OAuth2 authority Uri
            string authorityUri = "https://login.microsoftonline.com/common/";
         
            // AcquireToken will acquire an Azure access token
            // Call AcquireToken to get an Azure token from Azure Active Directory token issuance endpoint
            AuthenticationContext authContext = new AuthenticationContext(authorityUri);
            var credential = new ClientCredential(_clientId, _clientSecret);
            var authenticationResult = authContext.AcquireToken("https://analysis.windows.net/powerbi/api", credential);
            token = authenticationResult.AccessToken;

            HttpWebRequest request;
            var url = "https://api.powerbi.com/v1.0/myorg/datasets/{DATASETIDHERE}/refreshes/?$top=1";
            request = WebRequest.CreateHttp(url);
            request.Method = "GET";


            var tryItToken = "eyJ0eXAiO etc etc etc";

            //Add token to the request header
            request.Headers.Add("Authorization", String.Format("Bearer {0}", token));
            

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                dynamic array = JsonConvert.DeserializeObject(responseString);
            }

            return token;
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;tryItToken is populated with the token that is generated from here&amp;nbsp;&lt;A href="https://docs.microsoft.com/en-us/rest/api/power-bi/datasets/getrefreshhistory" target="_blank" rel="noopener"&gt;https://docs.microsoft.com/en-us/rest/api/power-bi/datasets/getrefreshhistory .&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, whenever I pass in the try it token, I get the repsonse back absolutely fine.&amp;nbsp; However, if I replace the tryItToken with the token I get through authenticationcontext.AcquireToken I am receiving the 401 Unauthorized.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone point me in the direction of the steps I have clearly missed out in getting this working correctly from within code?!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Sep 2020 07:49:08 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/REST-Api-Access-Token-The-remote-server-returned-an-error-401/m-p/1346664#M25546</guid>
      <dc:creator>MrRichDean</dc:creator>
      <dc:date>2020-09-04T07:49:08Z</dc:date>
    </item>
    <item>
      <title>Re: REST Api  - Access Token 'The remote server returned an error: (401) Unauthorized.'</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/REST-Api-Access-Token-The-remote-server-returned-an-error-401/m-p/1347782#M25553</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am also new. Does the AuthenticationContext fill out the authotityUri correct?&lt;/P&gt;&lt;P&gt;When I use curl I need to call &lt;A href="http://login.microsoftonline.com/common/oauth2/token" target="_blank"&gt;http://login.microsoftonline.com/common/oauth2/token.&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kind regards&lt;/P&gt;&lt;P&gt;Johan&lt;/P&gt;</description>
      <pubDate>Fri, 04 Sep 2020 13:49:10 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/REST-Api-Access-Token-The-remote-server-returned-an-error-401/m-p/1347782#M25553</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-09-04T13:49:10Z</dc:date>
    </item>
    <item>
      <title>Re: REST Api  - Access Token 'The remote server returned an error: (401) Unauthorized.'</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/REST-Api-Access-Token-The-remote-server-returned-an-error-401/m-p/1357284#M25633</link>
      <description>&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/256993"&gt;@MrRichDean&lt;/a&gt;,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;I'd like to suggest you take a look at the following tutorial about getting an access token if it works on your side:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;&lt;A href="https://docs.microsoft.com/en-us/power-bi/developer/automation/walkthrough-push-data-get-token" target="_self"&gt;Get an authentication access token&lt;/A&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Regards,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Xiaoxin Sheng&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Sep 2020 07:31:00 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/REST-Api-Access-Token-The-remote-server-returned-an-error-401/m-p/1357284#M25633</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-09-09T07:31:00Z</dc:date>
    </item>
    <item>
      <title>Re: REST Api  - Access Token 'The remote server returned an error: (401) Unauthorized.'</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/REST-Api-Access-Token-The-remote-server-returned-an-error-401/m-p/1357544#M25635</link>
      <description>&lt;P&gt;Good morning&amp;nbsp;&lt;SPAN&gt;@Anonymous&lt;/a&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I attempted to do this in a previous attempt but when I tried using the asnyc method, I received the following;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MrRichDean_0-1599641170739.png" style="width: 400px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/351964iD1539F4057D0C518/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MrRichDean_0-1599641170739.png" alt="MrRichDean_0-1599641170739.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Sep 2020 08:47:14 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/REST-Api-Access-Token-The-remote-server-returned-an-error-401/m-p/1357544#M25635</guid>
      <dc:creator>MrRichDean</dc:creator>
      <dc:date>2020-09-09T08:47:14Z</dc:date>
    </item>
    <item>
      <title>Re: REST Api  - Access Token 'The remote server returned an error: (401) Unauthorized.'</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/REST-Api-Access-Token-The-remote-server-returned-an-error-401/m-p/1360565#M25653</link>
      <description>&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/256993"&gt;@MrRichDean&lt;/a&gt;,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;It seems like the new version NuGet pack changes the defined so that the old version code not working. Please try to use the following code replaces the raw part if it works on your side:&lt;/FONT&gt;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;private static string GetToken()
        {
            // and add using Microsoft.IdentityModel.Clients.ActiveDirectory

            //The client id that Azure AD created when you registered your client app.
            string clientID = "{Client_ID}";

            //RedirectUri you used when you register your app.
            //For a client app, a redirect uri gives Azure AD more details on the application that it will authenticate.
            // You can use this redirect uri for your client app
            string redirectUri = "https://login.live.com/oauth20_desktop.srf";

            //Resource Uri for Power BI API
            string resourceUri = "https://analysis.windows.net/powerbi/api";

            //OAuth2 authority Uri
            string authorityUri = "https://login.microsoftonline.com/common/";

            //Get access token:
            // To call a Power BI REST operation, create an instance of AuthenticationContext and call AcquireToken
            // AuthenticationContext is part of the Active Directory Authentication Library NuGet package
            // To install the Active Directory Authentication Library NuGet package in Visual Studio,
            //  run "Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory" from the nuget Package Manager Console.

            // AcquireToken will acquire an Azure access token
            // Call AcquireToken to get an Azure token from Azure Active Directory token issuance endpoint
            AuthenticationContext authContext = new AuthenticationContext(authorityUri);

            string token = authContext.AcquireTokenAsync(resourceUri, clientID, new Uri(redirectUri), (IPlatformParameters)new UserCredential()).Result.AccessToken;

            Console.WriteLine(token);
            Console.ReadLine();

            return token;
        }&lt;/LI-CODE&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Regards,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Xiaoxin Sheng&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Sep 2020 07:21:28 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/REST-Api-Access-Token-The-remote-server-returned-an-error-401/m-p/1360565#M25653</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-09-10T07:21:28Z</dc:date>
    </item>
    <item>
      <title>Re: REST Api  - Access Token 'The remote server returned an error: (401) Unauthorized.'</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/REST-Api-Access-Token-The-remote-server-returned-an-error-401/m-p/1361283#M25660</link>
      <description>&lt;P&gt;Thank you for your hep&amp;nbsp;@Anonymous&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately it looks like the new definition doesn't like IPlatformParameters&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MrRichDean_0-1599738237063.png" style="width: 400px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/353028iAA888C2EF1F6CE35/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MrRichDean_0-1599738237063.png" alt="MrRichDean_0-1599738237063.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Sep 2020 11:44:06 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/REST-Api-Access-Token-The-remote-server-returned-an-error-401/m-p/1361283#M25660</guid>
      <dc:creator>MrRichDean</dc:creator>
      <dc:date>2020-09-10T11:44:06Z</dc:date>
    </item>
    <item>
      <title>Re: REST Api  - Access Token 'The remote server returned an error: (401) Unauthorized.'</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/REST-Api-Access-Token-The-remote-server-returned-an-error-401/m-p/1373727#M25731</link>
      <description>&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/256993"&gt;@MrRichDean&lt;/a&gt;,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;What version of the NuGet pack are you worked? I direct copy the NuGet pack name (from document) to vs studio package manager then downloads and test with the last version.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Regards,&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="tahoma,arial,helvetica,sans-serif"&gt;Xiaoxin Sheng&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Sep 2020 02:25:11 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/REST-Api-Access-Token-The-remote-server-returned-an-error-401/m-p/1373727#M25731</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-09-16T02:25:11Z</dc:date>
    </item>
  </channel>
</rss>

