W
Wur.Dip.Brightspace.AuthSdk
Wur.Dip.Brightspace.AuthSdk
Wur.Dip.Brightspace.AuthSdk is een NuGet package waarmee op een gemakkelijke manier de Brightspace authenticatie te gebruiken voor het gebruiken van de verschillende api's.
Installatie
- Voeg de WUR NuGet repository toe aan het project in Visual Studio.
(Tools -> NuGet Package Manager -> Package Manager Settings -> Package Sources)
https://nuget.wurnet.nl/nuget
- Het NuGet package kan nu worden toegevoegd aan het project.
Using
Zonder dependency injection:
BrightspaceSettings settings = new BrightspaceSettings()
{
Host = "wurnldev.brightspace.com",
HostScheme = "https",
HostPort = 443,
AppId = "xxxxxxxxx",
AppKey = "xxxxxxxx",
UserId = "xxxxxx",
UserKey = "xxxxxx"
};
Authenticator authenticator = new Authenticator(settings);
var url = authenticator.CreateAuthenticatedUri("/d2l/api/versions/", HttpMethod.Get);
using var response = await new HttpClient().GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
var json = await response.Content.ReadAsStringAsync();
Console.WriteLine(json);
Een voorbeeld met dependency injection:
- appsettings.json
"Brightspace": {
"HostScheme": "https",
"HostPort": 443,
"Host": "wurnldev.brightspace.com",
"AppId": "xxxxxxx",
"AppKey": "xxxxxxx",
"UserId": "xxxxxx",
"UserKey": "xxxxx"
}
- Startup.cs
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddBrightspaceConfiguration(Configuration);
services.AddHttpClient();
}
- HomeController.cs
[ApiController]
[Route("[controller]")]
public class HomeController : ControllerBase
{
private readonly HttpClient m_httpClient;
private readonly Authenticator m_authenticator;
public HomeController(IHttpClientFactory httpClient, IOptions<BrightspaceSettings> brightOptions)
{
m_httpClient = httpClient.CreateClient();
m_authenticator = new Authenticator(brightOptions.Value);
}
[HttpGet]
public async Task<ActionResult<string>> Get()
{
var url = m_authenticator.CreateAuthenticatedUri("/d2l/api/versions/", HttpMethod.Get);
using var response = await new HttpClient().GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
var json = await response.Content.ReadAsStringAsync();
return Ok(json);
}
}