I am having difficulty adding materials to a coursework object in c# and can't find any examples online other than in Python. My ultimate aim to add a form (I know you need to do this either with a link or drive file) but I've been experimenting with adding links first.
((To Test, you can create an appropriate Google project here and use the code below: https://developers.google.com/classroom/quickstart/dotnet))
I have a class as follows:
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;
}
}
}
}
In my main program I just call as follows:
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();
}
When I run this I get
System.NullReferenceException: 'Object reference not set to an instance of an object.'
If I remove the line below from the class function 'CreateAssignment' it works fine:
Materials = { materials },
You have to provide the materials to the API as a List object.
First define your materials as a list object:
var mats = new List<Material>() { materials };
And include it in your CourseWork object:
var assignment = new CourseWork
{
Title = "An Assignment",
Description = "Read the article about ant colonies and complete the quiz.",
Materials = mats,
WorkType = "ASSIGNMENT"
};