<?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: Data Refresh by using API: Need Steps in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/1038162#M23217</link>
    <description>&lt;P&gt;If we are using the&amp;nbsp;getAccessTokenSilently() function , do we really need to install the Microsoft.IdentityModel.Clients.Active Directory?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 21 Apr 2020 05:09:18 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2020-04-21T05:09:18Z</dc:date>
    <item>
      <title>Data Refresh by using API: Need Steps</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/208928#M6609</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I read in this article about data refresh thru API&lt;/P&gt;&lt;P&gt;&lt;A href="https://powerbi.microsoft.com/en-us/blog/announcing-data-refresh-apis-in-the-power-bi-service/" target="_blank"&gt;https://powerbi.microsoft.com/en-us/blog/announcing-data-refresh-apis-in-the-power-bi-service/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;but I do not know how to proceed further. I do not know anything about APIs, REST APIs etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you please help me out here to proceed further. What are the steps that I need to follow to execute below URL.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A title="" href="https://api.powerbi.com/v1.0/myorg/groups/%7bgroup_id%7d/datasets/%7bdataset_id%7d/refreshes" target="_blank"&gt;https://api.powerbi.com/v1.0/myorg/groups/{group_id}/datasets/{dataset_id}/refreshes&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jul 2017 15:10:34 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/208928#M6609</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-07-10T15:10:34Z</dc:date>
    </item>
    <item>
      <title>Re: Data Refresh by using API: Need Steps</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/209371#M6614</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;@Anonymous&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I read in this article about data refresh thru API&lt;/P&gt;
&lt;P&gt;&lt;A href="https://powerbi.microsoft.com/en-us/blog/announcing-data-refresh-apis-in-the-power-bi-service/" target="_blank"&gt;https://powerbi.microsoft.com/en-us/blog/announcing-data-refresh-apis-in-the-power-bi-service/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but I do not know how to proceed further. I do not know anything about APIs, REST APIs etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you please help me out here to proceed further. What are the steps that I need to follow to execute below URL.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A title="" href="https://api.powerbi.com/v1.0/myorg/groups/%7bgroup_id%7d/datasets/%7bdataset_id%7d/refreshes" target="_blank"&gt;https://api.powerbi.com/v1.0/myorg/groups/{group_id}/datasets/{dataset_id}/refreshes&lt;/A&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;@Anonymous&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;The APIs are development stuff so I hope you would have some coding skill. See a demo in C#. Do note that the REST API also has the schedule refresh limitation(8 times per day, if you'd like to lift this limitation, you may have to buy a premium license(48 times per day), see &lt;A href="https://powerbi.microsoft.com/en-us/blog/announcing-data-refresh-apis-in-the-power-bi-service/" target="_self"&gt;this link&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;using System;
using System.Net;
//Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.21.301221612
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Text;
//Install-Package Newtonsoft.Json 
using Newtonsoft.Json;
using System.IO;
using System.Web;
using System.Collections.Generic;
using System.Net.Http;
using System.Collections.Specialized;

namespace ConsoleApplication39
{

    class Program
    {

        //Step 1 - Replace {client id} with your client app ID. 
        //To learn how to get a client app ID, see Register a client app (https://msdn.microsoft.com/en-US/library/dn877542.aspx#clientID)
        private static string clientID = "49df1bc7-XXXXX9XXX0d1a4";

        //RedirectUri you used when you registered your app.
        //For a client app, a redirect uri gives AAD more details on the specific application that it will authenticate.
        private static string redirectUri = "https://login.live.com/oauth20_desktop.srf";

        //Resource Uri for Power BI API
        private static string resourceUri = "https://analysis.windows.net/powerbi/api";

        //OAuth2 authority Uri
        private static string authority = "https://login.windows.net/common/oauth2/authorize";

        //the account used to login Power BI
        private static string username = "MYACOUNT";
        private static string password = "MYPASSWD";

        private static AuthenticationContext authContext = null;
        private static string token = String.Empty;

        //The power bi app workspace id(the GUID after /groups/ in below link
        //when viewing a dataset in Power BI Service, the link is like
        //https://msit.powerbi.com/groups/dc581184-XXXXf16b6c15/datasets/1f6285a5-7XXX7b7ae5637d6
        private static string groupId = "dc581184-XXXXX-5432f16b6c15";

        //The target datasetId that is to refresh(the GUID after datesets/ in above link
        private static string datasetId = "1f6285a5XXXXXae5637d6";

        static void Main(string[] args)
        {

            //token = getAccessTokenWithLoginPopUp();
            token = getAccessTokenSilently();
            refreshDataset(groupId, datasetId);

            //wait 5 seconds for the last refresh
            System.Threading.Thread.Sleep(5000);

            getRefreshHistory(groupId, datasetId);

            Console.ReadKey();

        }



        static void getRefreshHistory(string groupId, string datasetId, int lastNRrefresh = 10)
        {

            HttpWebRequest request = System.Net.HttpWebRequest.CreateHttp(String.Format("https://api.powerbi.com/v1.0/myorg/groups/{0}/datasets/{1}/refreshes/?$top={2}", groupId, datasetId,lastNRrefresh));
            //POST web request to create a datasource.
            request.KeepAlive = true;
            request.Method = "GET";
            request.ContentLength = 0;

            //Add token to the request header
            request.Headers.Add("Authorization", String.Format("Bearer {0}", token));

            //Write JSON byte[] into a Stream
            using (var response = (HttpWebResponse)request.GetResponse())
            {
                 
                var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                dynamic responseJson = JsonConvert.DeserializeObject&amp;lt;dynamic&amp;gt;(responseString);
                foreach (var refresh in responseJson.value) {
                    Console.WriteLine("Dataset {0} refreshed is {1}",datasetId,refresh["status"]);
                    Console.WriteLine("starttime at {0} endtime at {1}", refresh["startTime"], refresh["endTime"]);
                    Console.WriteLine("");
                } 
            } 
        }


        static void refreshDataset(string groupId, string datasetId)
        {

            HttpWebRequest request = System.Net.HttpWebRequest.CreateHttp(String.Format("https://api.powerbi.com/v1.0/myorg/groups/{0}/datasets/{1}/refreshes", groupId, datasetId));
            //POST web request to create a datasource.
            request.KeepAlive = true;
            request.Method = "POST";
            request.ContentLength = 0;

            //Add token to the request header
            request.Headers.Add("Authorization", String.Format("Bearer {0}", token));

            //Write JSON byte[] into a Stream
            using (Stream writer = request.GetRequestStream())
            {

                var response = (HttpWebResponse)request.GetResponse();
                Console.WriteLine("Dataset refresh request {0}", response.StatusCode.ToString());
            }


        }

        static string getAccessTokenSilently()
        {

            HttpWebRequest request = System.Net.HttpWebRequest.CreateHttp("https://login.windows.net/common/oauth2/token");
            //POST web request to create a datasource.
            request.KeepAlive = true;
            request.Method = "POST";
            request.ContentLength = 0;
            request.ContentType = "application/x-www-form-urlencoded";

            //Add token to the request header
            request.Headers.Add("Authorization", String.Format("Bearer {0}", token));

            NameValueCollection parsedQueryString = HttpUtility.ParseQueryString(String.Empty);
            parsedQueryString.Add("client_id", clientID);
            parsedQueryString.Add("grant_type", "password");
            parsedQueryString.Add("resource", resourceUri);
            parsedQueryString.Add("username", username);
            parsedQueryString.Add("password", password);
            string postdata = parsedQueryString.ToString();


            //POST web request
            byte[] dataByteArray = System.Text.Encoding.ASCII.GetBytes(postdata); ;
            request.ContentLength = dataByteArray.Length;

            //Write JSON byte[] into a Stream
            using (Stream writer = request.GetRequestStream())
            {
                writer.Write(dataByteArray, 0, dataByteArray.Length);
                var response = (HttpWebResponse)request.GetResponse();
                var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                dynamic responseJson = JsonConvert.DeserializeObject&amp;lt;dynamic&amp;gt;(responseString);
                return responseJson["access_token"];
            }


        }


        static string getAccessTokenWithLoginPopUp()
        {
            if (token == String.Empty)
            {
                //Get Azure access token
                // Create an instance of TokenCache to cache the access token
                TokenCache TC = new TokenCache();
                // Create an instance of AuthenticationContext to acquire an Azure access token
                authContext = new AuthenticationContext(authority, TC);
                // Call AcquireToken to get an Azure token from Azure Active Directory token issuance endpoint
                token = authContext.AcquireToken(resourceUri, clientID, new Uri(redirectUri), PromptBehavior.RefreshSession).AccessToken;
            }
            else
            {
                // Get the token in the cache
                token = authContext.AcquireTokenSilent(resourceUri, clientID).AccessToken;
            }

            return token;
        }
    }
}
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 02:24:48 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/209371#M6614</guid>
      <dc:creator>Eric_Zhang</dc:creator>
      <dc:date>2017-07-20T02:24:48Z</dc:date>
    </item>
    <item>
      <title>Re: Data Refresh by using API: Need Steps</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/313048#M9258</link>
      <description>&lt;P&gt;What program is this code developed in?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Nov 2017 20:59:42 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/313048#M9258</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2017-11-28T20:59:42Z</dc:date>
    </item>
    <item>
      <title>Re: Data Refresh by using API: Need Steps</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/330932#M9803</link>
      <description>&lt;P&gt;Can this be used to refresh the reports that are imported to the Azure workspace&amp;nbsp;i.e. using Power BI Embedded?&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jan 2018 08:14:42 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/330932#M9803</guid>
      <dc:creator>zq</dc:creator>
      <dc:date>2018-01-01T08:14:42Z</dc:date>
    </item>
    <item>
      <title>Re: Data Refresh by using API: Need Steps</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/351812#M10469</link>
      <description>&lt;P&gt;i have implemented the above cited example, everything went smoothly, then i'm facing&amp;nbsp;&lt;/P&gt;&lt;P&gt;"Exception has been thrown by the target of an invocation. System: The remote server returned an error: (401) Unauthorized."&lt;/P&gt;&lt;P&gt;I'd made an azure function where my code is hosted and whenever i run that piece of code, above error occurs.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically i'm refreshing the power bi dataset through Power BI REST - API&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestion / help would be appreciated.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Feb 2018 15:49:42 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/351812#M10469</guid>
      <dc:creator>ahsanixc</dc:creator>
      <dc:date>2018-02-05T15:49:42Z</dc:date>
    </item>
    <item>
      <title>Re: Data Refresh by using API: Need Steps</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/353507#M10518</link>
      <description>&lt;P&gt;Did you figure this out? I am getting the same issue.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;wrote:&lt;BR /&gt;&lt;P&gt;i have implemented the above cited example, everything went smoothly, then i'm facing&amp;nbsp;&lt;/P&gt;&lt;P&gt;"Exception has been thrown by the target of an invocation. System: The remote server returned an error: (401) Unauthorized."&lt;/P&gt;&lt;P&gt;I'd made an azure function where my code is hosted and whenever i run that piece of code, above error occurs.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Basically i'm refreshing the power bi dataset through Power BI REST - API&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestion / help would be appreciated.&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Feb 2018 11:04:25 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/353507#M10518</guid>
      <dc:creator>kritika_goel</dc:creator>
      <dc:date>2018-02-07T11:04:25Z</dc:date>
    </item>
    <item>
      <title>Re: Data Refresh by using API: Need Steps</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/353525#M10519</link>
      <description>&lt;P&gt;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/54342"&gt;@kritika_goel&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used postman to see api call header and body but still status was same "&lt;/P&gt;&lt;P&gt;error 401 unauthorised.&amp;nbsp;"&lt;/P&gt;&lt;P&gt;Then i added client_secret as parameter and was able to obtain token via postman and status code was 200 (Connection established ), didn't tried via code.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Screenshot (208).png" style="width: 600px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/81085iB23085B2B411F4D3/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot (208).png" alt="Screenshot (208).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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Feb 2018 11:33:44 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/353525#M10519</guid>
      <dc:creator>ahsanixc</dc:creator>
      <dc:date>2018-02-07T11:33:44Z</dc:date>
    </item>
    <item>
      <title>Re: Data Refresh by using API: Need Steps</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/354375#M10539</link>
      <description>&lt;P&gt;I was also facing the same issue. The issue was i have installed latest version whereas version&amp;nbsp;2.21.301221612 was to be installed.&lt;/P&gt;&lt;PRE&gt;//Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 2.21.301221612&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 08 Feb 2018 07:49:51 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/354375#M10539</guid>
      <dc:creator>kritika_goel</dc:creator>
      <dc:date>2018-02-08T07:49:51Z</dc:date>
    </item>
    <item>
      <title>Re: Data Refresh by using API: Need Steps</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/1038162#M23217</link>
      <description>&lt;P&gt;If we are using the&amp;nbsp;getAccessTokenSilently() function , do we really need to install the Microsoft.IdentityModel.Clients.Active Directory?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 05:09:18 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Data-Refresh-by-using-API-Need-Steps/m-p/1038162#M23217</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-04-21T05:09:18Z</dc:date>
    </item>
  </channel>
</rss>

