Last active
April 30, 2019 00:48
-
-
Save dhinag/3c4cddc9c60a94d1b66d3079339ba859 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using(var httpClient = new HttpClient()) | |
{ | |
// Set headers | |
httpClient.Timeout = new TimeSpan(0, 2, 0); // 2 minutes | |
httpClient.BaseAddress = new Uri(_serviceUrl); | |
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0"); | |
httpClient.DefaultRequestHeaders.Accept.Add( | |
new MediaTypeWithQualityHeaderValue("application/json")); | |
// Use the bearer token here | |
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenResponse.Token); | |
HttpResponseMessage whoAmIResponse = await httpClient.GetAsync("api/data/v9.1/WhoAmI()"); | |
Guid userId; | |
if (whoAmIResponse.IsSuccessStatusCode) | |
{ | |
var jWhoAmIResponse = JObject.Parse(whoAmIResponse.Content.ReadAsStringAsync().Result); | |
// Finally, Get the userId | |
userId = (Guid)jWhoAmIResponse["UserId"]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment