Grade Sync API in Google Classroom Integration with ASP.NET Core 3.0 Web Application

Introduction

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

Using Google classroom StudentSubmission API

 
Step 1
 
Go to Views->googleClassRoom->Index.cshtml and add the below scope: https://www.googleapis.com/auth/classroom.coursework.students, and https://www.googleapis.com/auth/classroom.coursework.me in Google auth call. This scope is used to add a permission to view and manage the Google classroom classwork student grade information for the selected classwork.
  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 https://www.Googleapis.com/auth/classroom.coursework.me',  
  6.                  });  
  7.              });  
  8.          }  
Step 2
 
In my last article, we saw how to fetch the Google Classroom and classwork list using the Google classroom API. Now, based on the selected Google classroom and classwork in the grid, we are going to fetch the list of students with the grade that belongs to the selected Classwork using the Classwork API.
  1. <button type="button" id="btnGetClassworkGrade" class="k-button">Get ClassworkGrade</button>  
  1. $("#btnGetClassworkGrade").click(function () {  
  2.                 debugger;  
  3.                 var grid = $("#grid").data("kendoGrid");  
  4.                 var selectedItem = grid.dataItem(grid.select());  
  5.                 var grid = $("#classWorkGrid").data("kendoGrid");  
  6.                 var selectedClassWorkItem = grid.dataItem(grid.select());  
  7.                 $("#classWorkGradeGrid").kendoGrid({  
  8.                     dataSource: {  
  9.                         type: "json",  
  10.                         transport: {  
  11.                             read: "/GoogleClassRoom/GetClassWorkGrades?courseId=" + selectedItem.id  + '&' + "courseWorkId=" + selectedClassWorkItem.id  
  12.                         },  
  13.                         pageSize: 20  
  14.                     },  
  15.                     selectable: "true",  
  16.   
  17.                     toolbar: ["search"],  
  18.                     columns: [{  
  19.                         field: "userId",  
  20.                         title: "User ID"  
  21.                     },  
  22.                         {  
  23.                             field: "assignedGrade",  
  24.                             title:"Grade"  
  25.                         }]  
  26.                 });  
  27.   
  28.             })  
The above button click event is used to do the GetClassWorkGrades ajax call. It will trigger the Google Classroom ClassworkGrade API action, which is defined in GoogleClassroomController. Based on the response, the data will be rendered in the Grid.
 
GoogleClassroomController.cs
  1. public List<StudentSubmission> GetClassWorkGrades(string courseId, string courseWorkId)    
  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 classWorkGradeList = new List<StudentSubmission>();    
  30.             do    
  31.             {    
  32.                 var request = GoogleClassService.Courses.CourseWork.StudentSubmissions.List(courseId, courseWorkId);    
  33.                 request.PageSize = 100;    
  34.                 request.PageToken = pageToken;    
  35.                 var response = request.Execute();    
  36.                 if (response.StudentSubmissions != null)    
  37.                 {    
  38.                     classWorkGradeList.AddRange(response.StudentSubmissions);    
  39.                 }    
  40.                 pageToken = response.NextPageToken;    
  41.             } while (pageToken != null);    
  42.             return classWorkGradeList;    
  43.         }  
In the above code, you can see that the course ID and classwordId is provided as a parameter for the action GetClassWorkGrade. Based on the couseId and classworkId the list of students with grades available in ClassroomService will return the list of Student Submissions. In other words, the student list with grades is available to 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:
 
Click on the Get Course button. This will fetch the Google classroom course details using Get course API. The list of courses will be loaded in the grid.
 
 
Select a course from the list of courses in the grid. In my case, I selected Class C and clicked on the Get Classwork button. It will open the classwork grid, as shown in the below figure.
 
 
Select the coursework in the grid. In my case, I selected mathwork and clicked on the Get ClassworkGrade button. It will open the List of students available in the class with the grades of their respective classwork.
 
 
The student's grade 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 student classwork grade list by adding a new scope.
Click here for the source code.