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

How do we add materials to a Google Classroom API Coursework object?

我在用 c# 将材料添加到课程作业对象时遇到困难,并且除了 Python 之外在网上找不到任何示例。我的最终目标是添加一个表单(我知道你需要使用 link 或驱动器文件来完成此操作)但我一直在尝试先添加 links。

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

我有一个class如下:

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.'

如果我从 class 函数中删除下面的行 'CreateAssignment' 它工作正常:

Materials = { materials },

答案:

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

代码:

首先将您的材料定义为列表对象:

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

并将其包含在您的 CourseWork 对象中:

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

参考文献: