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; namespace RestApiSample { class Program { static void Main(string[] args) { CallRefreshAsync(); Console.ReadLine(); } private static async void CallRefreshAsync() { HttpClient client = new HttpClient(); //client.BaseAddress = new Uri("https://.asazure.windows.net/servers//models//"); client.BaseAddress = new Uri("https://southcentralus.asazure.windows.net/servers/chwade003/models/AdventureWorks0/"); // Send refresh request client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await UpdateToken()); RefreshRequest refreshRequest = new RefreshRequest() { type = "full", maxParallelism = 10 }; HttpResponseMessage response = await client.PostAsJsonAsync("refreshes", refreshRequest); response.EnsureSuccessStatusCode(); Uri location = response.Headers.Location; Console.WriteLine(response.Headers.Location); // Check the response while (true) // Will exit while loop when exit Main() method (it's running asynchronously) { string output = ""; // Refresh token if required client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", await UpdateToken()); response = await client.GetAsync(location); if (response.IsSuccessStatusCode) { output = await response.Content.ReadAsStringAsync(); } Console.Clear(); Console.WriteLine(output); Thread.Sleep(5000); } } private static async Task UpdateToken() { string resourceURI = "https://*.asazure.windows.net"; string authority = "https://login.windows.net//oauth2/authorize"; AuthenticationContext ac = new AuthenticationContext(authority); #region 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)); //Username/password: //UserPasswordCredential cred = new UserPasswordCredential("", ""); //AuthenticationResult ar = await ac.AcquireTokenAsync(resourceURI, clientID, cred); #endregion //Service principal: ClientCredential cred = new ClientCredential("", ""); AuthenticationResult ar = await ac.AcquireTokenAsync(resourceURI, cred); return ar.AccessToken; } } class RefreshRequest { public string type { get; set; } public int maxParallelism { get; set; } } }