我们如何向 Google 课堂 API 课程作业对象添加材料?



我在 c# 中向课程对象添加材料时遇到困难,并且除了 Python 之外,无法在线找到任何示例。我的最终目标是添加一个表单(我知道您需要使用链接或驱动器文件执行此操作(,但我一直在尝试先添加链接。

((要进行测试,您可以在此处创建一个适当的Google项目并使用下面的代码:https://developers.google.com/classroom/quickstart/dotnet((

我有一个课程如下:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Classroom.v1;
using Google.Apis.Classroom.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Windows;
namespace Google
{
class cGClassroom
{
ClassroomService service;
static string[] Scopes =
{
ClassroomService.Scope.ClassroomAnnouncements,
ClassroomService.Scope.ClassroomCourses,
ClassroomService.Scope.ClassroomCourseworkStudents,
ClassroomService.Scope.ClassroomProfileEmails,
ClassroomService.Scope.ClassroomProfilePhotos,
ClassroomService.Scope.ClassroomRosters
};
static string ApplicationName = "ESL Suite";

public void EstablishServiceConnection()
{
//Setup credentials
UserCredential credential;
try
{
using (var stream =
new FileStream("Resources\GoogleAPI\credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
MessageBox.Show("Credential file saved to: " + credPath);
}
// Create Classroom API service.
service = new ClassroomService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
}
catch (Exception e)
{
MessageBox.Show(e.InnerException.Message);
}
}
public void CreateClass()
{
var course = new Course
{
Name = "L2 English",
Section = "Class 2.1",
DescriptionHeading = "Welcome to L2 Pre-Intermediate English",
Description = "We'll be studying a variety of topics and grammar points "
+ "using a combination of the core textbook, online materials, and in-class time. Expect "
+ "to study hard!",
Room = "304.B10",
OwnerId = "me",
CourseState = "PROVISIONED"
};
course = service.Courses.Create(course).Execute();
Console.WriteLine("Course created: {0} ({1})", course.Name, course.Id);
MessageBox.Show("Course created: " + course.Name + " " + course.Id);
}
public void CreateAssignment()
{
string courseId = "*************";
var link = new Link
{
Title = "Google",
Url = "http://www.google.com"
};
var materials = new Material
{
Link = link
};
var assignment = new CourseWork
{
Title = "An Assignment",
Description = "Read the article about ant colonies and complete the quiz.",
Materials = { materials },
WorkType = "ASSIGNMENT"
};
try
{
assignment = service.Courses.CourseWork.Create(assignment, courseId).Execute();
}
catch (GoogleApiException e)
{
throw e;
}
}
}
}

在我的主程序中,我只是按如下方式调用:

private void bCreateGoogleClassroom_Click(object sender, RoutedEventArgs e)
{
cGClassroom oClassroom = new cGClassroom();
oClassroom.EstablishServiceConnection();
//Comment out once class ID is know - Testing purposes
//oClassroom.CreateClass();
oClassroom.CreateAssignment();
}

当我运行这个时,我得到

System.NullReferenceException: 'Object reference not set to an instance of an object.'

如果我从类函数"创建分配"中删除下面的行,它可以正常工作:

Materials = { materials },

答案:

您必须将材料作为列表对象提供给 API。

法典:

首先将材质定义为列表对象:

var mats = new List<Material>() { materials };

并将其包含在您的课程作业对象中:

var assignment = new CourseWork
{
Title = "An Assignment",
Description = "Read the article about ant colonies and complete the quiz.",
Materials = mats,
WorkType = "ASSIGNMENT"
};

引用:

  • 教室: Google.Apis.Classroom.v1.Data.CourseWork 课堂参考