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,
- $("#btnSendInvite").click(function () {
- var sendInviteList = [];
- var studentList = $("#studentList").val().split(',');
- for (var i = 0; i < studentList.length; i++) {
- var student = new Student()
- student.StudentID = i;
- student.EmailID = studentList[i]
- sendInviteList.push(student);
- }
- var grid = $("#grid").data("kendoGrid");
- var selectedItem = grid.dataItem(grid.select());
- sendInvite(sendInviteList, selectedItem.id);
- });
-
- function sendInvite(sentInviteList, ClassId) {
- var param = {
- StudentList: sentInviteList,
- CourseId: ClassId,
- Role: "STUDENT",
- }
- $.ajax({
- url: "/GoogleClassRoom/SendInvite",
- data: JSON.stringify(param),
- type: "POST",
- contentType: "application/json; charset=utf-8",
- success: function (response) {
- $("#inviteWindow").data("kendoWindow").close();
- },
- error: function (xhr) {
-
- }
- })
-
- }
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
- public ActionResult SendInvite([FromBody]GoogleInvite param)
- {
- var Token = GetAuthenticator();
- var token = new TokenResponse()
- {
- AccessToken = Token,
- ExpiresInSeconds = 3600,
- IssuedUtc = DateTime.UtcNow
- };
-
- var authflow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
- {
- ClientSecrets = new ClientSecrets
- {
- ClientId = ClientId,
- ClientSecret = ClientSecret
- }
- });
-
- var credential = new UserCredential(authflow, "0", token);
- var serviceInitializer = new BaseClientService.Initializer()
- {
- ApplicationName = GoogleApplicationName,
- HttpClientInitializer = credential
- };
-
- var googleClassService = new ClassroomService(serviceInitializer);
- foreach (var student in param.StudentList)
- {
- Invitation invitation = new Invitation();
- invitation.CourseId = param.CourseId;
- invitation.Role = "STUDENT";
- invitation.UserId = student.EmailID;
- var request = googleClassService.Invitations.Create(invitation);
- try
- {
- var response = request.Execute();
- }
- catch (Exception ex)
- {
- }
- }
-
- return Json("");
-
- }
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.
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.
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.
- Invitation invitation = new Invitation();
- invitation.CourseId = param.CourseId;
- invitation.Role = "STUDENT";
- invitation.UserId = student.EmailID;
Step 3
Provide email address and click on Send Invite, this will send an invite to the student email address.
Click on join, it will take you to google classroom to join the course as a student.
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.