Using Get Classwork API In Google Classroom Integration With ASP.NET Core 3.0 Web Application

Introduction

 
In my last article we saw how to add scope in the Google classroom API integrated with ASP.NET Core 3.0 web applications. In this article you will learn how to fetch the Google classroom classwork details based on the selected class using the Classwork API in an ASP.NET Core web application.
 
This is a continuation of my last article.
 

Using Classwork API

 
Step 1
 
Go to Views->GoogleClassRoom->Index.cshtml, add the below scope https://www.googleapis.com/auth/classroom.coursework.students in Google auth call. This scope is used to add a permission to view and manage the Google classroom classwork information for the selected classrooms.
  1. function start() {  
  2.               gapi.load('auth2'function () {  
  3.                   auth2 = gapi.auth2.init({  
  4.                       client_id: "1070617910386-e614kn8nptnf7kpec2fmksoaa3t610gd.apps.googleusercontent.com",  
  5.                       scope: 'https://www.googleapis.com/auth/classroom.courses.readonly https://www.googleapis.com/auth/classroom.coursework.students',  
  6.                   });  
  7.               });  
  8.           }  
Step 2
 
In my last article saw how to fetch the google classroom list using the google classroom API, now based on the selected google classroom in the grid we are going to fetch the list of Google Classroom Classwork using the Classwork API.
  1. <button type="button" id="btnGetClasswork" class="k-button">Get Classwork</button>  
  2.   <div id="classWorkGrid"></div>  
  1. $("#btnGetClasswork").click(function () {  
  2.                  var grid = $("#grid").data("kendoGrid");  
  3.                 var selectedItem = grid.dataItem(grid.select());  
  4.                 $("#classWorkGrid").kendoGrid({  
  5.                     dataSource: {  
  6.                         type: "json",  
  7.                         transport: {  
  8.                             read: "/GoogleClassRoom/GetClassWork?courseId="+ selectedItem.id  
  9.                         },  
  10.                         pageSize: 20  
  11.                     },  
  12.                     selectable: "true",  
  13.   
  14.                     toolbar: ["search"],  
  15.                     columns: [{  
  16.                         field: "title",  
  17.                         title: "Name"  
  18.                     },]  
  19.                 });  
  20.   
  21.             })  
The above button click event is used to do the GetClassWork ajax call. It will trigger the Google Classroom Classwork API action which is defined in GoogleClassroomController. Based on the response the data will be rendered in the Grid.
 
GoogleClassroomController.cs 
  1. public List<CourseWork>  GetClassWork(string courseId)  
  2.         {  
  3.             var Token = GetAuthenticator();  
  4.             var token = new TokenResponse()  
  5.             {  
  6.                 AccessToken = Token,  
  7.                 ExpiresInSeconds = 3600,  
  8.                 IssuedUtc = DateTime.UtcNow  
  9.             };  
  10.   
  11.             var authflow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer  
  12.             {  
  13.                 ClientSecrets = new ClientSecrets  
  14.                 {  
  15.                     ClientId = ClientId,  
  16.                     ClientSecret = ClientSecret  
  17.                 }  
  18.             });  
  19.   
  20.             var credential = new UserCredential(authflow, "0", token);  
  21.             var serviceInitializer = new BaseClientService.Initializer()  
  22.             {  
  23.                 ApplicationName = GoogleApplicationName,  
  24.                 HttpClientInitializer = credential  
  25.             };  
  26.   
  27.             var googleClassService = new ClassroomService(serviceInitializer);  
  28.             string pageToken = null;  
  29.             var classWorkList = new List<CourseWork>();  
  30.             do  
  31.             {  
  32.                 var request = googleClassService.Courses.CourseWork.List(courseId);  
  33.                 request.PageSize = 100;  
  34.                 request.PageToken = pageToken;  
  35.                 var response = request.Execute();  
  36.                 if (response.CourseWork != null)  
  37.                 {  
  38.                     classWorkList.AddRange(response.CourseWork);  
  39.                 }  
  40.                 pageToken = response.NextPageToken;  
  41.             } while (pageToken != null);  
  42.             return classWorkList;  
  43.         }  
From the above code you can notice the course id is provided as a parameter for the action GetClassWork. Based the couseId the CourseWork in ClassroomService will return the list of CourseWork available in the class.
 
Step 3
 
Run the application, navigate to /GoogleClassRoom/Index in browser.
 
Since we added a new scope, click on the sign in button and complete the authentication process:
 
Using Get Classwork API In Google Classroom Integration With ASP.NET Core 3.0 Web Application
Using Get Classwork API In Google Classroom Integration With ASP.NET Core 3.0 Web Application
 
Click on allow to grant the permission.
 
Click on Get Course button, this will fetch the Google classroom course details using Get course API, and the list of courses will be loaded in the grid.
 
Select course from the list of courses in grid, in my case I selected Class C, and click on Get Classwork button. It will open the classwork grid as shown in the below figure. 
 
Using Get Classwork API In Google Classroom Integration With ASP.NET Core 3.0 Web Application
 
Select course from the list of courses in the grid, in my case I selected Class C, and click on Get Classwork button. It will open the classwork grid as shown in the below figure.
 
Using Get Classwork API In Google Classroom Integration With ASP.NET Core 3.0 Web Application
 
MathsWeeklyTest is the classwork which is available in the class C.
 
Log in to Google classroom using the same account 
 
Using Get Classwork API In Google Classroom Integration With ASP.NET Core 3.0 Web Application
 
The classwork MathsWeeklyTest is in Google classroom. 
 
Reference
 
https://developers.google.com/classroom/quickstart/dotnet 
 

Summary

 
We have gone through,
  • Integrating the Google Classroom classwork API with our ASP.NET Core web application.
  • How to manage the Google classroom service to fetch the classwork list by adding a new classroom.coursework.students scope.
Click here for the source code.