From 3da7610b5597b80f48bf91d6928f1d82370d6be0 Mon Sep 17 00:00:00 2001 From: Kajetan Kazimierczak Date: Wed, 20 Jul 2022 00:46:05 +0200 Subject: [PATCH 1/2] Added Microsoft.Graph nuget. Can get info about student from graph. --- src/SkolplattformenElevApi/ApiMsGraph.cs | 77 +++++++++++++++++++ .../Models/MsGraph/OauthTokenResponse.cs | 50 ++++++++++++ .../SkolplattformenElevApi.csproj | 6 +- src/SkolplattformenElevDemo/Program.cs | 47 ++++++----- 4 files changed, 158 insertions(+), 22 deletions(-) create mode 100644 src/SkolplattformenElevApi/ApiMsGraph.cs create mode 100644 src/SkolplattformenElevApi/Models/MsGraph/OauthTokenResponse.cs diff --git a/src/SkolplattformenElevApi/ApiMsGraph.cs b/src/SkolplattformenElevApi/ApiMsGraph.cs new file mode 100644 index 0000000..5215e2b --- /dev/null +++ b/src/SkolplattformenElevApi/ApiMsGraph.cs @@ -0,0 +1,77 @@ +using System.Net; +using System.Net.Http.Headers; +using System.Text.Json; +using System.Text.RegularExpressions; +using Microsoft.Graph; +using SkolplattformenElevApi.Models.MsGraph; +using SkolplattformenElevApi.Models.Timetable; + +namespace SkolplattformenElevApi; + +public partial class Api +{ + + public async Task GetMsGraphAccessTokenAsync() + { + var tokenResponse = await GetOauthTokenResponseAsync(OauthTokenResource.MsGraph); + + return tokenResponse.AccessToken; + } + + + private async Task GetOauthTokenResponseAsync(string tokenResource) + { + var temp_url = "https://elevstockholm.sharepoint.com/sites/skolplattformen/_api/SP.OAuth.Token/Acquire"; + var content = "{\"resource\":\"" + tokenResource + "\"}"; + var request = new HttpRequestMessage + { + RequestUri = new Uri(temp_url), + Method = HttpMethod.Post, + Headers = + { + { "Odata-version", "3.0"}, + { "Referer", "https://elevstockholm.sharepoint.com/sites/skolplattformen/" }, + { "X-Scope", "8a22163c-8662-4535-9050-bc5e1923df48" }, + { "Accept", "application/json"}, + { "Origin", "https://elevstockholm.sharepoint.com" }, + { "X-RequestDigest", _formDigestValue} + }, + Content = new StringContent(content) + }; + request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); + + + var temp_res = await _httpClient.SendAsync(request); + + var temp_content = await temp_res.Content.ReadAsStringAsync(); + + var jsonSerializerOptions = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }; + + var obj = JsonSerializer.Deserialize(temp_content, jsonSerializerOptions); + + return obj; + + } + + public async Task GetMeAsync() + { + + var accessToken = await GetMsGraphAccessTokenAsync(); + + + var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => { + requestMessage + .Headers + .Authorization = new AuthenticationHeaderValue("Bearer", accessToken); + + return Task.CompletedTask; + })); + + var me = await graphServiceClient.Me.Request().GetAsync(); + + return me.DisplayName; + } +} diff --git a/src/SkolplattformenElevApi/Models/MsGraph/OauthTokenResponse.cs b/src/SkolplattformenElevApi/Models/MsGraph/OauthTokenResponse.cs new file mode 100644 index 0000000..00c27ef --- /dev/null +++ b/src/SkolplattformenElevApi/Models/MsGraph/OauthTokenResponse.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace SkolplattformenElevApi.Models.MsGraph; +// Root myDeserializedClass = JsonConvert.DeserializeObject(myJsonResponse); +internal class OauthTokenResponse +{ + [JsonPropertyName("@odata.context")] + public string OdataContext { get; set; } + + [JsonPropertyName("@odata.type")] + public string OdataType { get; set; } + + [JsonPropertyName("@odata.id")] + public string OdataId { get; set; } + + [JsonPropertyName("@odata.editLink")] + public string OdataEditLink { get; set; } + + [JsonPropertyName("access_token")] + public string AccessToken { get; set; } + + [JsonPropertyName("expires_on")] + public string ExpiresOn { get; set; } + + [JsonPropertyName("id_token")] + public object IdToken { get; set; } + + [JsonPropertyName("resource")] + public string Resource { get; set; } + + [JsonPropertyName("scope")] + public string Scope { get; set; } + + [JsonPropertyName("token_type")] + public string TokenType { get; set; } +} + +internal static class OauthTokenResource +{ + public static string Bing => "https://www.bing.com"; + public static string OutlookSeatch => "https://outlook.office365.com/search"; + public static string Loki => "https://loki.delve.office.com"; + public static string MsGraph => "https://graph.microsoft.com"; + +} \ No newline at end of file diff --git a/src/SkolplattformenElevApi/SkolplattformenElevApi.csproj b/src/SkolplattformenElevApi/SkolplattformenElevApi.csproj index 132c02c..e6f5428 100644 --- a/src/SkolplattformenElevApi/SkolplattformenElevApi.csproj +++ b/src/SkolplattformenElevApi/SkolplattformenElevApi.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -6,4 +6,8 @@ enable + + + + diff --git a/src/SkolplattformenElevDemo/Program.cs b/src/SkolplattformenElevDemo/Program.cs index 09ee9ff..22e4570 100644 --- a/src/SkolplattformenElevDemo/Program.cs +++ b/src/SkolplattformenElevDemo/Program.cs @@ -11,34 +11,39 @@ var api = new Api(); await api.LogIn(elev.Email, elev.Username, elev.Password); -var itemList = await api.GetNewsItemList(5); +//var itemList = await api.GetNewsItemList(5); -Console.WriteLine("----- News -----"); -foreach (var newsListItem in itemList) -{ - Console.WriteLine($"{newsListItem.Title} | {newsListItem.ModifiedBy} | {newsListItem.Path}"); -} +//Console.WriteLine("----- News -----"); +//foreach (var newsListItem in itemList) +//{ +// Console.WriteLine($"{newsListItem.Title} | {newsListItem.ModifiedBy} | {newsListItem.Path}"); +//} -await api.GetNewsItem(itemList[1].Path); +//await api.GetNewsItem(itemList[1].Path); -Console.WriteLine("----- Planned Absence -----"); +//Console.WriteLine("----- Planned Absence -----"); -await api.AbsenceSsoLogin(); -var absenceList = await api.GetPlannedAbsenceList(); -foreach (var a in absenceList) -{ - Console.WriteLine($"{a.DateTimeFrom.Date} {a.ReasonDescription} ({a.Reporter})"); -} +//await api.AbsenceSsoLogin(); +//var absenceList = await api.GetPlannedAbsenceList(); +//foreach (var a in absenceList) +//{ +// Console.WriteLine($"{a.DateTimeFrom.Date} {a.ReasonDescription} ({a.Reporter})"); +//} -Console.WriteLine("----- Timetable ------"); +//Console.WriteLine("----- Timetable ------"); -await api.TimetableSsoLogin(); -var lessonInfo = await api.GetTimetable(2022, 37); +//await api.TimetableSsoLogin(); +//var lessonInfo = await api.GetTimetable(2022, 37); -foreach (var info in lessonInfo) -{ - Console.WriteLine($"{info.DayOfWeekNumber} {info.TimeStart}-{info.TimeEnd}: {info.Texts[0]} {info.Texts[1]} {info.Texts[2]} "); -} +//foreach (var info in lessonInfo) +//{ +// Console.WriteLine($"{info.DayOfWeekNumber} {info.TimeStart}-{info.TimeEnd}: {info.Texts[0]} {info.Texts[1]} {info.Texts[2]} "); +//} + +//var token = await api.GetMsGraphBearerToken(); + +var me = await api.GetMeAsync(); +Console.WriteLine($"Me: {me}"); class ElevInfo { From fa78731ccbba7c00b4d779436b7661872c5281a1 Mon Sep 17 00:00:00 2001 From: Kajetan Kazimierczak Date: Sat, 23 Jul 2022 00:22:03 +0200 Subject: [PATCH 2/2] typo --- src/SkolplattformenElevApi/Models/MsGraph/OauthTokenResponse.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SkolplattformenElevApi/Models/MsGraph/OauthTokenResponse.cs b/src/SkolplattformenElevApi/Models/MsGraph/OauthTokenResponse.cs index 00c27ef..67f36e8 100644 --- a/src/SkolplattformenElevApi/Models/MsGraph/OauthTokenResponse.cs +++ b/src/SkolplattformenElevApi/Models/MsGraph/OauthTokenResponse.cs @@ -43,7 +43,7 @@ internal class OauthTokenResponse internal static class OauthTokenResource { public static string Bing => "https://www.bing.com"; - public static string OutlookSeatch => "https://outlook.office365.com/search"; + public static string OutlookSearch => "https://outlook.office365.com/search"; public static string Loki => "https://loki.delve.office.com"; public static string MsGraph => "https://graph.microsoft.com";