From 17dceefcb837c467acd77ea2bec72b213ad9a533 Mon Sep 17 00:00:00 2001 From: Christian Wade Date: Tue, 14 Dec 2021 13:21:31 -0800 Subject: [PATCH] Power BI async refresh REST API sample --- RestApiSample/RestApiSample/Program.cs | 44 ++++++++++++++++++-------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/RestApiSample/RestApiSample/Program.cs b/RestApiSample/RestApiSample/Program.cs index d5a74e2..8214a18 100644 --- a/RestApiSample/RestApiSample/Program.cs +++ b/RestApiSample/RestApiSample/Program.cs @@ -1,10 +1,6 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; using Microsoft.IdentityModel.Clients.ActiveDirectory; -using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Threading; @@ -22,8 +18,11 @@ namespace RestApiSample private static async void CallRefreshAsync() { HttpClient client = new HttpClient(); + //AAS template //client.BaseAddress = new Uri("https://.asazure.windows.net/servers//models//"); - client.BaseAddress = new Uri("https://southcentralus.asazure.windows.net/servers/chwade003/models/AdventureWorks0/"); + + //PBI template + client.BaseAddress = new Uri("https://api.powerbi.com/v1.0/myorg/groups//datasets//"); // Send refresh request client.DefaultRequestHeaders.Accept.Clear(); @@ -37,6 +36,7 @@ namespace RestApiSample }; HttpResponseMessage response = await client.PostAsJsonAsync("refreshes", refreshRequest); + string content = await response.Content.ReadAsStringAsync(); response.EnsureSuccessStatusCode(); Uri location = response.Headers.Location; Console.WriteLine(response.Headers.Location); @@ -64,25 +64,41 @@ namespace RestApiSample private static async Task UpdateToken() { - string resourceURI = "https://*.asazure.windows.net"; - string authority = "https://login.windows.net//oauth2/authorize"; - AuthenticationContext ac = new AuthenticationContext(authority); + // AAS REST API Inputs: + // string resourceURI = "https://*.asazure.windows.net"; + // string authority = "https://login.windows.net//oauth2/authorize"; + // AuthenticationContext ac = new AuthenticationContext(authority); - #region Interactive or username/password + // PBI REST API Inputs: + string resourceURI = "https://analysis.windows.net/powerbi/api"; + string authority = "https://login.microsoftonline.com/"; + string[] scopes = new string[] { $"{resourceURI}/.default" }; + + + + #region Use Interactive or username/password //string clientID = ""; // Native app with necessary API permissions //Interactive login if not cached: - //AuthenticationResult ar = await ac.AcquireTokenAsync(resourceURI, clientID, new Uri("urn:ietf:wg:oauth:2.0:oob"), new PlatformParameters(PromptBehavior.Auto)); + //AuthenticationContext ac = new AuthenticationContext(authority); + //AuthenticationResult ar = await ac.AcquireTokenAsync(resourceURI, clientID, new Uri("urn:ietf:wg:oauth:2.0:oob"), new PlatformParameters(PromptBehavior.SelectAccount)); - //Username/password: - //UserPasswordCredential cred = new UserPasswordCredential("", ""); - //AuthenticationResult ar = await ac.AcquireTokenAsync(resourceURI, clientID, cred); + // Username/password: + // AuthenticationContext ac = new AuthenticationContext(authority); + // UserPasswordCredential cred = new UserPasswordCredential("", ""); + // AuthenticationResult ar = await ac.AcquireTokenAsync(resourceURI, clientID, cred); #endregion - //Service principal: + // AAS Service Principal: + // ClientCredential cred = new ClientCredential("", ""); + // AuthenticationResult ar = await ac.AcquireTokenAsync(resourceURI, cred); + + + // PBI Service Principal: + AuthenticationContext ac = new AuthenticationContext(authority); ClientCredential cred = new ClientCredential("", ""); AuthenticationResult ar = await ac.AcquireTokenAsync(resourceURI, cred);