#:property PublishAot=false using System.Net.Http.Headers; using System.Text; using System.Text.Json; static string Required(string name) => Environment.GetEnvironmentVariable(name) ?? throw new InvalidOperationException("Set " + name); using var client = new HttpClient(new HttpClientHandler { AllowAutoRedirect = false }) { BaseAddress = new Uri("https://scormbox.com/api/v1/"), Timeout = TimeSpan.FromSeconds(20) }; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(Required("SCORMBOX_API_KEY") + ":" + Required("SCORMBOX_API_SECRET")))); async Task Request(string path, object? body = null) { using var message = new HttpRequestMessage(body is null ? HttpMethod.Get : HttpMethod.Post, path); if (body is not null) message.Content = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json"); using var response = await client.SendAsync(message); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); } var course_path = "courses/" + Uri.EscapeDataString(Required("SCORMBOX_COURSE_ID")); var user = new { name = Required("SCORMBOX_LEARNER_NAME"), email = Required("SCORMBOX_LEARNER_EMAIL") }; using var session = JsonDocument.Parse(await Request(course_path + "/sessions", new { user, origin = Required("SCORMBOX_ORIGIN"), attempt_mode = "resume" })); // Deliver session.RootElement.GetProperty("launch_token").GetString() only to this learner's authorized page. var result = await Request(course_path + "/results?email=" + Uri.EscapeDataString(user.email)); Console.WriteLine(result);