我们如何向Google Classroom API课件对象添加材料?

问题描述 投票:0回答:1

我很难用c#将材料添加到课件对象中,除了Python之外,网上找不到任何例子。我的最终目的是添加一个表单(我知道你需要用链接或驱动文件来实现),但我一直在尝试先添加链接。

((为了测试,你可以在这里创建一个合适的Google项目,并使用下面的代码。https:/developers.google.comclassroomquickstartdotnet))

我有一个类如下。

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

如果我从类函数'CreateAssignment'中删除下面的一行,就可以正常工作。

Materials = { materials },

如果有任何帮助,我将非常感激

保罗

c# google-classroom
1个回答
2
投票

答案是

你必须把材料以List对象的形式提供给API。

代码:首先将你的材料定义为一个List对象。

首先定义你的材料为一个列表对象。

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"
            };

我希望这对你有帮助

参考文献。

© www.soinside.com 2019 - 2024. All rights reserved.