/// <summary> /// 4. Display results with link to view the course /// </summary> /// <returns></returns> public async Task <IActionResult> View(CancellationToken cancellationToken, CourseWorkModel model) { var appFlow = new AppFlowMetadata(ClientId, ClientSecret, _context); var token = await appFlow.Flow.LoadTokenAsync(model.UserId, cancellationToken); var credential = new UserCredential(appFlow.Flow, model.UserId, token); try { // Get information using Google Classroom API using (var classroomService = new ClassroomService(new BaseClientService.Initializer { HttpClientInitializer = credential, ApplicationName = "gc2lti" })) { var request = classroomService.Courses.Get(model.CourseId); var response = await request.ExecuteAsync(cancellationToken).ConfigureAwait(false); model.CourseAlternativeLink = response.AlternateLink; return(View(model)); } } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e)); } }
/// <summary> /// 3. Create the assignment /// </summary> public async Task <IActionResult> Assign(CancellationToken cancellationToken, CourseWorkModel model) { var appFlow = new AppFlowMetadata(ClientId, ClientSecret, _context); var token = await appFlow.Flow.LoadTokenAsync(model.UserId, cancellationToken); var credential = new UserCredential(appFlow.Flow, model.UserId, token); try { // Create the assignment using (var classroomService = new ClassroomService(new BaseClientService.Initializer { HttpClientInitializer = credential, ApplicationName = "gc2lti" })) { var nonce = CalculateNonce(8); var linkUrl = IsRequestLocal() ? $"{_configuration["Localhost"]}/gc2lti/{nonce}?u={model.Url}&c={model.CourseId}&t={model.Title}" : $"{_configuration["Remotehost"]}/gc2lti/{nonce}?u={model.Url}&c={model.CourseId}&t={model.Title}"; var courseWork = new CourseWork { Title = model.Title, Description = model.Description, MaxPoints = model.MaxPoints, WorkType = "ASSIGNMENT", Materials = new List <Material> { new Material { Link = new Link { Title = model.Title, // The title will be overwritten by Google's thumbnail generator Url = linkUrl } } }, State = "PUBLISHED" }; var createRequest = classroomService.Courses.CourseWork.Create(courseWork, model.CourseId); courseWork = await createRequest.ExecuteAsync(cancellationToken).ConfigureAwait(false); model.CourseWorkId = courseWork.Id; return(RedirectToAction("View", model)); } } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e)); } }