Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/SkolplattformenElevApi/ApiMsGraph.cs
Original file line number Diff line number Diff line change
@@ -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<string> GetMsGraphAccessTokenAsync()
{
var tokenResponse = await GetOauthTokenResponseAsync(OauthTokenResource.MsGraph);

return tokenResponse.AccessToken;
}


private async Task<OauthTokenResponse> 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<OauthTokenResponse>(temp_content, jsonSerializerOptions);

return obj;

}

public async Task<string> 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;
}
}
50 changes: 50 additions & 0 deletions src/SkolplattformenElevApi/Models/MsGraph/OauthTokenResponse.cs
Original file line number Diff line number Diff line change
@@ -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<Root>(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 OutlookSearch => "https://outlook.office365.com/search";
public static string Loki => "https://loki.delve.office.com";
public static string MsGraph => "https://graph.microsoft.com";

}
6 changes: 5 additions & 1 deletion src/SkolplattformenElevApi/SkolplattformenElevApi.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Graph" Version="4.34.0" />
</ItemGroup>

</Project>
47 changes: 26 additions & 21 deletions src/SkolplattformenElevDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down