Created
July 29, 2020 21:01
-
-
Save ejazhussain/d84ce456f313e7dfa021c3a63c64201d to your computer and use it in GitHub Desktop.
ASP.NET Core - Microsoft Graph API Service
This file contains hidden or 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
| public class GraphService : IGraphService | |
| { | |
| private readonly string serviceEndpoint = "https://graph.microsoft.com/v1.0"; | |
| public async Task<UserInfo> GetUserProfileAsync(string accessToken) | |
| { | |
| UserInfo profile = new UserInfo(); | |
| try | |
| { | |
| HttpClient client = new HttpClient(); | |
| client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken); | |
| // Endpoint for my profile | |
| Uri usersEndpoint = new Uri(serviceEndpoint + "/me"); | |
| HttpResponseMessage response = await client.GetAsync(usersEndpoint); | |
| if (response.IsSuccessStatusCode) | |
| { | |
| string responseContent = await response.Content.ReadAsStringAsync(); | |
| JObject jResult = JObject.Parse(responseContent); | |
| profile.DisplayName = jResult["displayName"].ToString(); | |
| profile.JobTitle = jResult["jobTitle"].ToString(); | |
| profile.Email = jResult["mail"].ToString(); | |
| profile.OfficeLocation = jResult["officeLocation"].ToString(); | |
| profile.MobilePhone = jResult["mobilePhone"].ToString(); | |
| } | |
| else | |
| { | |
| return null; | |
| } | |
| } | |
| catch (Exception e) | |
| { | |
| return null; | |
| } | |
| return profile; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment