public static async Task <UserProfile> GetUserProfileRequest() { UserProfile userProfile = null; string errorMessage = string.Empty; try { // Obtain information for communicating with the service: Office365ServiceInfo serviceInfo = await Office365ServiceInfo.GetActiveDirectoryServiceInfoAsync(); if (!serviceInfo.HasValidAccessToken) { throw new Exception("Unable to get AAD ServiceInfo"); } // Create a URL for retrieving the data: string[] queryParameters = { "api-version=2013-11-08" }; string requestUrl = String.Format(CultureInfo.InvariantCulture, "{0}/me?{1}", serviceInfo.ApiEndpoint, String.Join("&", queryParameters)); // Prepare the HTTP request: using (HttpClient client = new HttpClient()) { Func <HttpRequestMessage> requestCreator = () => { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl); request.Headers.Add("Accept", "application/json;odata=minimalmetadata"); return(request); }; using (HttpResponseMessage response = await Office365Helper.SendRequestAsync( serviceInfo, client, requestCreator)) { // Read the response and deserialize the data: string responseString = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { await Office365Helper.ShowErrorMessageAsync(serviceInfo, responseString); return(userProfile); } userProfile = JsonConvert.DeserializeObject <UserProfile>(responseString); } } } catch (Exception ex) { errorMessage = ex.Message; } return(userProfile); }
private static async Task <SampleModel.ActiveDirectory.UserProfile> GetActiveDirectoryProfile() { // Obtain information for communicating with the service: Office365ServiceInfo serviceInfo = await Office365ServiceInfo.GetActiveDirectoryServiceInfoAsync(); if (!serviceInfo.HasValidAccessToken) { return(null); } // Create a URL for retrieving the data: string[] queryParameters = { "api-version=2013-11-08" }; string requestUrl = String.Format(CultureInfo.InvariantCulture, "{0}/{1}/users/{2}?{3}", serviceInfo.ApiEndpoint, WebUtility.UrlEncode(Office365Helper.TenantId), WebUtility.UrlEncode(Office365Helper.UserId), String.Join("&", queryParameters)); // Prepare the HTTP request: using (HttpClient client = new HttpClient()) { Func <HttpRequestMessage> requestCreator = () => { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl); request.Headers.Add("Accept", "application/json;odata=minimalmetadata"); return(request); }; // Send the request using a helper method, which will add an authorization header to the request, // and automatically retry with a new token if the existing one has expired. using (HttpResponseMessage response = await Office365Helper.SendRequestAsync( serviceInfo, client, requestCreator)) { // Read the response and deserialize the data: string responseString = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { await Office365Helper.ShowErrorMessageAsync(serviceInfo, responseString); return(null); } return(JsonConvert.DeserializeObject <SampleModel.ActiveDirectory.UserProfile>(responseString)); } } }