Using Send Invite API In Google Classroom Integration With ASP.NET Core 3.0 Web Application

Introduction

 
In my last article we have seen how to add scope in Google classroom API integrated with ASP.NET Core 3.0 web applications. In this article you will learn how to send an invite to the students to join class in Google classroom using send invite API using ASP.NET Core web application.
 
This is a continuation of my last article.
 

Using send invite API

 
Go to View->GoogleClassRoom->Index.cshtml, add below function, 
  1. $("#btnSendInvite").click(function () {                
  2.               var sendInviteList = [];  
  3.               var studentList = $("#studentList").val().split(',');  
  4.               for (var i = 0; i < studentList.length; i++) {  
  5.                   var student = new Student()  
  6.                   student.StudentID = i;  
  7.                   student.EmailID = studentList[i]  
  8.                   sendInviteList.push(student);  
  9.               }  
  10.               var grid = $("#grid").data("kendoGrid");  
  11.               var selectedItem = grid.dataItem(grid.select());  
  12.               sendInvite(sendInviteList, selectedItem.id);  
  13.           });  
  14.   
  15.    function sendInvite(sentInviteList, ClassId) {  
  16.               var param = {  
  17.                   StudentList: sentInviteList,  
  18.                   CourseId: ClassId,  
  19.                   Role: "STUDENT",  
  20.               }  
  21.               $.ajax({  
  22.                   url: "/GoogleClassRoom/SendInvite",  
  23.                   data: JSON.stringify(param),  
  24.                   type: "POST",  
  25.                   contentType: "application/json; charset=utf-8",  
  26.                   success: function (response) {  
  27.                       $("#inviteWindow").data("kendoWindow").close();  
  28.                   },  
  29.                   error: function (xhr) {  
  30.   
  31.                   }  
  32.               })  
  33.   
  34.           }  
The above button click event is used to do the SendInvite ajax call. It will trigger the SendInvite API action which is defined in GoogleClassroomController.
 
GoogleClassroomController.cs
  1. public ActionResult SendInvite([FromBody]GoogleInvite param)  
  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.     foreach (var student in param.StudentList)  
  29.     {  
  30.         Invitation invitation = new Invitation();  
  31.         invitation.CourseId = param.CourseId;  
  32.         invitation.Role = "STUDENT";  
  33.         invitation.UserId = student.EmailID;  
  34.         var request = googleClassService.Invitations.Create(invitation);  
  35.         try  
  36.         {  
  37.             var response = request.Execute();  
  38.         }  
  39.         catch (Exception ex)  
  40.         {  
  41.         }  
  42.     }  
  43.   
  44.     return Json("");  
  45.   
  46. }   
From the above code you can notice the student list with course id is provided as a parameter for the action SendInvite, the Invitation class is initialized and the invitations create function in ClassroomService is used to send an invite to the student based on the email Id in invitation object.
 
Run the application, navigate to /GoogleClassRoom/Index in browser.
 
Step 1
 
Click on 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.
Using Send Invite API In Google Classroom Integration With ASP.NET Core 3.0 Web Application
Step 2
 
Select course from the list of courses in grid, in my case I selected English and click on send invite button. It will open the popup window as shown in the below figure.
 
Using Send Invite API In Google Classroom Integration With ASP.NET Core 3.0 Web Application 
 
Using Send Invite API In Google Classroom Integration With ASP.NET Core 3.0 Web Application
 
Provide email address in the text box and click on send invite. This will trigger the sendInvite action in GoogleClassRoomController. From the student list, provide the course id and student email information to the google classroom invite service and make sure the Role property is assigned as “STUDENT” as given in below code.
  1.               Invitation invitation = new Invitation();  
  2.               invitation.CourseId = param.CourseId;  
  3.               invitation.Role = "STUDENT";  
  4.               invitation.UserId = student.EmailID;  
Step 3
 
Provide email address and click on Send Invite, this will send an invite to the student email address.
 
Using Send Invite API In Google Classroom Integration With ASP.NET Core 3.0 Web Application 
Using Send Invite API In Google Classroom Integration With ASP.NET Core 3.0 Web Application
 
Click on join, it will take you to google classroom to join the course as a student. 
Using Send Invite API In Google Classroom Integration With ASP.NET Core 3.0 Web Application 
Note
The email address should be registered with Google domain service. Probably the schools which are using this google classroom will be using the GSuite services for Education.
 
Reference
 
https://developers.google.com/classroom/quickstart/dotnet
 

Summary

 
We have gone through,
  • Integrating the Google Classroom send Invite API with our ASP.NET Core web application.
  • How to handle the Google classroom service to send invitation to the student email address.
Click here for the source code.