Skip to content

Instantly share code, notes, and snippets.

@dhinag
Last active April 30, 2019 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhinag/3c4cddc9c60a94d1b66d3079339ba859 to your computer and use it in GitHub Desktop.
Save dhinag/3c4cddc9c60a94d1b66d3079339ba859 to your computer and use it in GitHub Desktop.
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