diff --git a/classroom/snippets/src/main/java/AcceptInvitation.java b/classroom/snippets/src/main/java/AcceptInvitation.java new file mode 100644 index 00000000..137a23a0 --- /dev/null +++ b/classroom/snippets/src/main/java/AcceptInvitation.java @@ -0,0 +1,72 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// [START classroom_accept_invitation] + +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; + +/* Class to demonstrate the use of Classroom Accept Invitation API. */ +public class AcceptInvitation { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + + /** + * Accepts an invitation to a course. + * + * @param id - the identifier of the invitation to accept. + * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. + */ + public static void acceptInvitation(String id) throws GeneralSecurityException, IOException { + + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); + + // [START classroom_accept_invitation_code_snippet] + try { + service.invitations().accept(id).execute(); + System.out.printf("Invitation (%s) was accepted.\n", id); + } catch (GoogleJsonResponseException e) { + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The invitation id (%s) does not exist.\n", id); + } + throw e; + } catch (Exception e) { + throw e; + } + // [END classroom_accept_invitation_code_snippet] + } +} +// [END classroom_accept_invitation] diff --git a/classroom/snippets/src/main/java/AddAliasToCourse.java b/classroom/snippets/src/main/java/AddAliasToCourse.java index 10434391..720dccec 100644 --- a/classroom/snippets/src/main/java/AddAliasToCourse.java +++ b/classroom/snippets/src/main/java/AddAliasToCourse.java @@ -12,61 +12,61 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_add_alias_to_course_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.CourseAlias; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Create Alias API. */ public class AddAliasToCourse { + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + /** * Add an alias on an existing course. * * @param courseId - id of the course to add an alias to. * @return - newly created course alias. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static CourseAlias addAliasToCourse(String courseId) throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSES)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static CourseAlias addAliasToCourse(String courseId) + throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_add_alias_to_course_code_snippet] /* Create a new CourseAlias object with a project-wide alias. Project-wide aliases use a prefix of "p:" and can only be seen and used by the application that created them. */ - CourseAlias content = new CourseAlias() - .setAlias("p:biology_10"); + CourseAlias content = new CourseAlias().setAlias("p:biology_10"); CourseAlias courseAlias = null; try { - courseAlias = service.courses().aliases().create(courseId, content) - .execute(); + courseAlias = service.courses().aliases().create(courseId, content).execute(); System.out.printf("Course alias created: %s \n", courseAlias.getAlias()); } catch (GoogleJsonResponseException e) { - //TODO (developer) - handle error appropriately + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 409) { System.out.printf("The course alias already exists: %s.\n", content); diff --git a/classroom/snippets/src/main/java/AddStudent.java b/classroom/snippets/src/main/java/AddStudent.java index c04c38fb..6331f3ed 100644 --- a/classroom/snippets/src/main/java/AddStudent.java +++ b/classroom/snippets/src/main/java/AddStudent.java @@ -12,57 +12,64 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_add_student] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Student; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Add Student API */ public class AddStudent { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + /** * Add a student in a specified course. * - * @param courseId - Id of the course. + * @param courseId - Id of the course. * @param enrollmentCode - Code of the course to enroll. * @return newly added student * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static Student addStudent(String courseId, String enrollmentCode) - throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_ROSTERS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static Student addStudent(String courseId, String enrollmentCode, String studentId) + throws GeneralSecurityException, IOException { - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); - Student student = new Student().setUserId("gduser1@workspacesamples.dev"); + Student student = new Student().setUserId(studentId); try { // Enrolling a student to a specified course - student = service.courses().students().create(courseId, student) - .setEnrollmentCode(enrollmentCode) - .execute(); + student = + service + .courses() + .students() + .create(courseId, student) + .setEnrollmentCode(enrollmentCode) + .execute(); // Prints the course id with the Student name - System.out.printf("User '%s' was enrolled as a student in the course with ID '%s'.\n", + System.out.printf( + "User '%s' was enrolled as a student in the course with ID '%s'.\n", student.getProfile().getName().getFullName(), courseId); } catch (GoogleJsonResponseException e) { // TODO(developer) - handle error appropriately @@ -78,4 +85,4 @@ public static Student addStudent(String courseId, String enrollmentCode) return student; } } -// [END classroom_add_student] \ No newline at end of file +// [END classroom_add_student] diff --git a/classroom/snippets/src/main/java/AddTeacher.java b/classroom/snippets/src/main/java/AddTeacher.java index 9f5652e3..4c843721 100644 --- a/classroom/snippets/src/main/java/AddTeacher.java +++ b/classroom/snippets/src/main/java/AddTeacher.java @@ -12,55 +12,58 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_add_teacher] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Teacher; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Add Teacher API */ public class AddTeacher { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + /** * Add teacher to a specific course. * - * @param courseId - Id of the course. + * @param courseId - Id of the course. * @param teacherEmail - Email address of the teacher. * @return newly created teacher * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ public static Teacher addTeacher(String courseId, String teacherEmail) - throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_ROSTERS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + throws GeneralSecurityException, IOException { - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); Teacher teacher = new Teacher().setUserId(teacherEmail); try { // Add a teacher to a specified course teacher = service.courses().teachers().create(courseId, teacher).execute(); // Prints the course id with the teacher name - System.out.printf("User '%s' was added as a teacher to the course with ID '%s'.\n", + System.out.printf( + "User '%s' was added as a teacher to the course with ID '%s'.\n", teacher.getProfile().getName().getFullName(), courseId); } catch (GoogleJsonResponseException e) { // TODO(developer) - handle error appropriately @@ -76,4 +79,4 @@ public static Teacher addTeacher(String courseId, String teacherEmail) return teacher; } } -// [END classroom_add_teacher] \ No newline at end of file +// [END classroom_add_teacher] diff --git a/classroom/snippets/src/main/java/BatchAddStudents.java b/classroom/snippets/src/main/java/BatchAddStudents.java index 9588ec3d..d4a54c2e 100644 --- a/classroom/snippets/src/main/java/BatchAddStudents.java +++ b/classroom/snippets/src/main/java/BatchAddStudents.java @@ -12,62 +12,66 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_batch_add_students] import com.google.api.client.googleapis.batch.BatchRequest; import com.google.api.client.googleapis.batch.json.JsonBatchCallback; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.http.HttpHeaders; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Student; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /* Class to demonstrate the use of Classroom Batch Add Students API */ public class BatchAddStudents { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + /** * Add multiple students in a specified course. * - * @param courseId - Id of the course to add students. + * @param courseId - Id of the course to add students. * @param studentEmails - Email address of the students. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ public static void batchAddStudents(String courseId, List studentEmails) - throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_ROSTERS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + throws GeneralSecurityException, IOException { - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); BatchRequest batch = service.batch(); - JsonBatchCallback callback = new JsonBatchCallback<>() { - public void onSuccess(Student student, HttpHeaders responseHeaders) { - System.out.printf("User '%s' was added as a student to the course.\n", - student.getProfile().getName().getFullName()); - } + JsonBatchCallback callback = + new JsonBatchCallback<>() { + public void onSuccess(Student student, HttpHeaders responseHeaders) { + System.out.printf( + "User '%s' was added as a student to the course.\n", + student.getProfile().getName().getFullName()); + } - public void onFailure(GoogleJsonError error, HttpHeaders responseHeaders) { - System.out.printf("Error adding student to the course: %s\n", error.getMessage()); - } - }; + public void onFailure(GoogleJsonError error, HttpHeaders responseHeaders) { + System.out.printf("Error adding student to the course: %s\n", error.getMessage()); + } + }; for (String studentEmail : studentEmails) { Student student = new Student().setUserId(studentEmail); service.courses().students().create(courseId, student).queue(batch, callback); @@ -75,4 +79,4 @@ public void onFailure(GoogleJsonError error, HttpHeaders responseHeaders) { batch.execute(); } } -// [END classroom_batch_add_students] \ No newline at end of file +// [END classroom_batch_add_students] diff --git a/classroom/snippets/src/main/java/CreateCourse.java b/classroom/snippets/src/main/java/CreateCourse.java index ce33dd4d..95f7353c 100644 --- a/classroom/snippets/src/main/java/CreateCourse.java +++ b/classroom/snippets/src/main/java/CreateCourse.java @@ -12,59 +12,64 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_create_course] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Course; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Create Course API */ public class CreateCourse { + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + /** * Creates a course * * @return newly created course * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static Course createCourse() throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSES)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static Course createCourse() throws GeneralSecurityException, IOException { - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); Course course = null; try { - // Adding a new course with description - course = new Course() - .setName("10th Grade Biology") - .setSection("Period 2") - .setDescriptionHeading("Welcome to 10th Grade Biology") - .setDescription("We'll be learning about about the structure of living creatures " - + "from a combination of textbooks, guest lectures, and lab work. Expect " - + "to be excited!") - .setRoom("301") - .setOwnerId("me") - .setCourseState("PROVISIONED"); + // Adding a new course with description. Set CourseState to `ACTIVE`. Possible values of + // CourseState can be found here: + // https://developers.google.com/classroom/reference/rest/v1/courses#coursestate + course = + new Course() + .setName("10th Grade Biology") + .setSection("Period 2") + .setDescriptionHeading("Welcome to 10th Grade Biology") + .setDescription( + "We'll be learning about about the structure of living creatures " + + "from a combination of textbooks, guest lectures, and lab work. Expect " + + "to be excited!") + .setRoom("301") + .setOwnerId("me") + .setCourseState("ACTIVE"); course = service.courses().create(course).execute(); // Prints the new created course Id and name System.out.printf("Course created: %s (%s)\n", course.getName(), course.getId()); @@ -79,4 +84,4 @@ public static Course createCourse() throws IOException { return course; } } -// [END classroom_create_course] \ No newline at end of file +// [END classroom_create_course] diff --git a/classroom/snippets/src/main/java/CreateCourseWithAlias.java b/classroom/snippets/src/main/java/CreateCourseWithAlias.java index 77a6521b..c676b973 100644 --- a/classroom/snippets/src/main/java/CreateCourseWithAlias.java +++ b/classroom/snippets/src/main/java/CreateCourseWithAlias.java @@ -12,44 +12,47 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_create_course_with_alias_class] + +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Course; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate how to create a course with an alias. */ public class CreateCourseWithAlias { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + /** * Create a new course with an alias. Set the new course id to the desired alias. * * @return - newly created course. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static Course createCourseWithAlias() throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSES)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static Course createCourseWithAlias() throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_create_course_with_alias_code_snippet] @@ -57,20 +60,21 @@ public static Course createCourseWithAlias() throws IOException { /* Create a new Course with the alias set as the id field. Project-wide aliases use a prefix of "p:" and can only be seen and used by the application that created them. */ - Course content = new Course() - .setId("p:history_4_2022") - .setName("9th Grade History") - .setSection("Period 4") - .setDescriptionHeading("Welcome to 9th Grade History.") - .setOwnerId("me") - .setCourseState("PROVISIONED"); + Course content = + new Course() + .setId("p:history_4_2022") + .setName("9th Grade History") + .setSection("Period 4") + .setDescriptionHeading("Welcome to 9th Grade History.") + .setOwnerId("me") + .setCourseState("PROVISIONED"); try { course = service.courses().create(content).execute(); // Prints the new created course id and name System.out.printf("Course created: %s (%s)\n", course.getName(), course.getId()); } catch (GoogleJsonResponseException e) { - //TODO (developer) - handle error appropriately + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 409) { System.out.printf("The course alias already exists: %s.\n", content.getId()); diff --git a/classroom/snippets/src/main/java/CreateCourseWork.java b/classroom/snippets/src/main/java/CreateCourseWork.java index 7f9973ca..5d6c948d 100644 --- a/classroom/snippets/src/main/java/CreateCourseWork.java +++ b/classroom/snippets/src/main/java/CreateCourseWork.java @@ -12,60 +12,61 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_create_coursework_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.CourseWork; -import com.google.api.services.classroom.model.Date; import com.google.api.services.classroom.model.Link; import com.google.api.services.classroom.model.Material; -import com.google.api.services.classroom.model.TimeOfDay; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; /* Class to demonstrate the use of Classroom Create CourseWork API. */ public class CreateCourseWork { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + /** * Creates course work. * * @param courseId - id of the course to create coursework in. * @return - newly created CourseWork object. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static CourseWork createCourseWork(String courseId) throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static CourseWork createCourseWork(String courseId) + throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_create_coursework_code_snippet] CourseWork courseWork = null; try { // Create a link to add as a material on course work. - Link articleLink = new Link() - .setTitle("SR-71 Blackbird") - .setUrl("https://www.lockheedmartin.com/en-us/news/features/history/blackbird.html"); + Link articleLink = + new Link() + .setTitle("SR-71 Blackbird") + .setUrl("https://www.lockheedmartin.com/en-us/news/features/history/blackbird.html"); // Create a list of Materials to add to course work. List materials = Arrays.asList(new Material().setLink(articleLink)); @@ -75,21 +76,22 @@ public static CourseWork createCourseWork(String courseId) throws IOException { https://developers.google.com/classroom/reference/rest/v1/CourseWorkType Set state to `PUBLISHED`. Possible values of state can be found here: https://developers.google.com/classroom/reference/rest/v1/courses.courseWork#courseworkstate */ - CourseWork content = new CourseWork() - .setTitle("Supersonic aviation") - .setDescription("Read about how the SR-71 Blackbird, the world’s fastest and " - + "highest-flying manned aircraft, was built.") - .setMaterials(materials) - .setWorkType("ASSIGNMENT") - .setState("PUBLISHED"); + CourseWork content = + new CourseWork() + .setTitle("Supersonic aviation") + .setDescription( + "Read about how the SR-71 Blackbird, the world’s fastest and " + + "highest-flying manned aircraft, was built.") + .setMaterials(materials) + .setWorkType("ASSIGNMENT") + .setState("PUBLISHED"); - courseWork = service.courses().courseWork().create(courseId, content) - .execute(); + courseWork = service.courses().courseWork().create(courseId, content).execute(); /* Prints the created courseWork. */ System.out.printf("CourseWork created: %s\n", courseWork.getTitle()); } catch (GoogleJsonResponseException e) { - //TODO (developer) - handle error appropriately + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { System.out.printf("The courseId does not exist: %s.\n", courseId); @@ -105,4 +107,4 @@ public static CourseWork createCourseWork(String courseId) throws IOException { // [END classroom_create_coursework_code_snippet] } } -// [END classroom_create_coursework_class] \ No newline at end of file +// [END classroom_create_coursework_class] diff --git a/classroom/snippets/src/main/java/CreateInvitation.java b/classroom/snippets/src/main/java/CreateInvitation.java new file mode 100644 index 00000000..3e3d22ac --- /dev/null +++ b/classroom/snippets/src/main/java/CreateInvitation.java @@ -0,0 +1,89 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// [START classroom_create_invitation] + +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import com.google.api.services.classroom.model.Invitation; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; + +/* Class to demonstrate the use of Classroom Create Invitation API. */ +public class CreateInvitation { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + + /** + * Create an invitation to allow a user to join a course. + * + * @param courseId - the course to invite the user to. + * @param userId - the user to be invited to the course. + * @return the created invitation. + * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. + */ + public static Invitation createInvitation(String courseId, String userId) + throws GeneralSecurityException, IOException { + + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); + + // [START classroom_create_invitation_code_snippet] + + Invitation invitation = null; + try { + /* Set the role the user is invited to have in the course. Possible values of CourseRole can be + found here: https://developers.google.com/classroom/reference/rest/v1/invitations#courserole.*/ + Invitation content = + new Invitation().setCourseId(courseId).setUserId(userId).setRole("TEACHER"); + + invitation = service.invitations().create(content).execute(); + + System.out.printf( + "User (%s) has been invited to course (%s).\n", + invitation.getUserId(), invitation.getCourseId()); + } catch (GoogleJsonResponseException e) { + // TODO (developer) - handle error appropriately + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The course or user does not exist.\n"); + } + throw e; + } catch (Exception e) { + throw e; + } + return invitation; + + // [END classroom_create_invitation_code_snippet] + } +} +// [END classroom_create_invitation] diff --git a/classroom/snippets/src/main/java/CreateTopic.java b/classroom/snippets/src/main/java/CreateTopic.java index a46e573f..1be964a3 100644 --- a/classroom/snippets/src/main/java/CreateTopic.java +++ b/classroom/snippets/src/main/java/CreateTopic.java @@ -12,46 +12,48 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_create_topic_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Topic; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate how to create a topic. */ public class CreateTopic { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); + /** * Create a new topic in a course. * * @param courseId - the id of the course to create a topic in. * @return - newly created topic. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static Topic createTopic(String courseId) throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_TOPICS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static Topic createTopic(String courseId) throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_create_topic_code_snippet] @@ -62,7 +64,7 @@ public static Topic createTopic(String courseId) throws IOException { topic = service.courses().topics().create(courseId, content).execute(); System.out.println("Topic id: " + topic.getTopicId() + "\n" + "Course id: " + courseId); } catch (GoogleJsonResponseException e) { - //TODO (developer) - handle error appropriately + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { System.out.printf("The courseId does not exist: %s.\n", courseId); diff --git a/classroom/snippets/src/main/java/DeleteInvitation.java b/classroom/snippets/src/main/java/DeleteInvitation.java new file mode 100644 index 00000000..eca59e92 --- /dev/null +++ b/classroom/snippets/src/main/java/DeleteInvitation.java @@ -0,0 +1,72 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// [START classroom_delete_invitation] + +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; + +/* Class to demonstrate the use of Classroom Delete Invitation API. */ +public class DeleteInvitation { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + + /** + * Deletes an invitation. + * + * @param id - the identifier of the invitation to delete. + * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. + */ + public static void deleteInvitation(String id) throws GeneralSecurityException, IOException { + + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); + + // [START classroom_delete_invitation_code_snippet] + try { + service.invitations().delete(id).execute(); + System.out.printf("Invitation (%s) was deleted.\n", id); + } catch (GoogleJsonResponseException e) { + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The invitation id (%s) does not exist.\n", id); + } + throw e; + } catch (Exception e) { + throw e; + } + // [END classroom_delete_invitation_code_snippet] + } +} +// [END classroom_delete_invitation] diff --git a/classroom/snippets/src/main/java/DeleteTopic.java b/classroom/snippets/src/main/java/DeleteTopic.java index b8ee8f73..15188de6 100644 --- a/classroom/snippets/src/main/java/DeleteTopic.java +++ b/classroom/snippets/src/main/java/DeleteTopic.java @@ -12,23 +12,27 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_delete_topic_class] -import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate how to delete a topic. */ public class DeleteTopic { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); + /** * Delete a topic in a course. * @@ -36,22 +40,20 @@ public class DeleteTopic { * @param topicId - the id of the topic to delete. * @return - updated topic. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static void deleteTopic(String courseId, String topicId) throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_TOPICS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static void deleteTopic(String courseId, String topicId) + throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_delete_topic_code_snippet] @@ -59,12 +61,7 @@ public static void deleteTopic(String courseId, String topicId) throws IOExcepti service.courses().topics().delete(courseId, topicId).execute(); } catch (GoogleJsonResponseException e) { // TODO(developer) - handle error appropriately - GoogleJsonError error = e.getDetails(); - if (error.getCode() == 404) { - System.out.printf("The courseId or topicId does not exist: %s, %s.\n", courseId, topicId); - } else { - throw e; - } + throw e; } catch (Exception e) { throw e; } diff --git a/classroom/snippets/src/main/java/GetCourse.java b/classroom/snippets/src/main/java/GetCourse.java index 7955f1da..94d07f75 100644 --- a/classroom/snippets/src/main/java/GetCourse.java +++ b/classroom/snippets/src/main/java/GetCourse.java @@ -12,46 +12,48 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_get_course] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Course; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Get Course API */ public class GetCourse { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + /** * Retrieve a single course's metadata. * * @param courseId - Id of the course to return. * @return a course * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static Course getCourse(String courseId) throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSES)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static Course getCourse(String courseId) throws GeneralSecurityException, IOException { - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); Course course = null; try { @@ -69,4 +71,4 @@ public static Course getCourse(String courseId) throws IOException { return course; } } -// [END classroom_get_course] \ No newline at end of file +// [END classroom_get_course] diff --git a/classroom/snippets/src/main/java/GetInvitation.java b/classroom/snippets/src/main/java/GetInvitation.java new file mode 100644 index 00000000..77d0598f --- /dev/null +++ b/classroom/snippets/src/main/java/GetInvitation.java @@ -0,0 +1,76 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// [START classroom_get_invitation] + +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; +import com.google.api.client.googleapis.json.GoogleJsonError; +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.services.classroom.Classroom; +import com.google.api.services.classroom.ClassroomScopes; +import com.google.api.services.classroom.model.Invitation; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; + +/* Class to demonstrate the use of Classroom Get Invitation API. */ +public class GetInvitation { + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS)); + + /** + * Retrieves an invitation. + * + * @param id - the identifier of the invitation to retrieve. + * @return the specified invitation. + * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. + */ + public static Invitation getInvitation(String id) throws GeneralSecurityException, IOException { + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); + + // [START classroom_get_invitation_code_snippet] + Invitation invitation = null; + try { + invitation = service.invitations().get(id).execute(); + System.out.printf( + "Invitation (%s) for user (%s) in course (%s) retrieved.\n", + invitation.getId(), invitation.getUserId(), invitation.getCourseId()); + } catch (GoogleJsonResponseException e) { + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf("The invitation id (%s) does not exist.\n", id); + } + throw e; + } catch (Exception e) { + throw e; + } + return invitation; + // [END classroom_get_invitation_code_snippet] + } +} +// [END classroom_get_invitation] diff --git a/classroom/snippets/src/main/java/GetTopic.java b/classroom/snippets/src/main/java/GetTopic.java index 140a0407..5fb42012 100644 --- a/classroom/snippets/src/main/java/GetTopic.java +++ b/classroom/snippets/src/main/java/GetTopic.java @@ -12,24 +12,29 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_get_topic_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Topic; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate how to get a topic. */ public class GetTopic { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); + /** * Get a topic in a course. * @@ -37,22 +42,20 @@ public class GetTopic { * @param topicId - the id of the topic to retrieve. * @return - the topic to retrieve. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static Topic getTopic(String courseId, String topicId) throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_TOPICS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static Topic getTopic(String courseId, String topicId) + throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_get_topic_code_snippet] @@ -62,13 +65,12 @@ public static Topic getTopic(String courseId, String topicId) throws IOException topic = service.courses().topics().get(courseId, topicId).execute(); System.out.printf("Topic '%s' found.\n", topic.getName()); } catch (GoogleJsonResponseException e) { - //TODO (developer) - handle error appropriately + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { System.out.printf("The courseId or topicId does not exist: %s, %s.\n", courseId, topicId); - } else { - throw e; } + throw e; } catch (Exception e) { throw e; } diff --git a/classroom/snippets/src/main/java/ListCourseAliases.java b/classroom/snippets/src/main/java/ListCourseAliases.java index c3141cf1..dbec2299 100644 --- a/classroom/snippets/src/main/java/ListCourseAliases.java +++ b/classroom/snippets/src/main/java/ListCourseAliases.java @@ -12,50 +12,51 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_list_aliases_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.CourseAlias; import com.google.api.services.classroom.model.ListCourseAliasesResponse; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.ArrayList; -import java.util.Collections; +import java.util.Arrays; import java.util.List; /* Class to demonstrate the use of Classroom List Alias API */ public class ListCourseAliases { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + /** * Retrieve the aliases for a course. * * @param courseId - id of the course. * @return list of course aliases * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ public static List listCourseAliases(String courseId) - throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSES)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + throws GeneralSecurityException, IOException { - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_list_aliases_code_snippet] @@ -65,10 +66,14 @@ public static List listCourseAliases(String courseId) try { // List of aliases of specified course do { - ListCourseAliasesResponse response = service.courses().aliases().list(courseId) - .setPageSize(100) - .setPageToken(pageToken) - .execute(); + ListCourseAliasesResponse response = + service + .courses() + .aliases() + .list(courseId) + .setPageSize(100) + .setPageToken(pageToken) + .execute(); courseAliases.addAll(response.getAliases()); pageToken = response.getNextPageToken(); } while (pageToken != null); @@ -95,4 +100,4 @@ public static List listCourseAliases(String courseId) // [END classroom_list_aliases_code_snippet] } } -// [END classroom_list_aliases_class] \ No newline at end of file +// [END classroom_list_aliases_class] diff --git a/classroom/snippets/src/main/java/ListCourses.java b/classroom/snippets/src/main/java/ListCourses.java index 5b7bd58f..c5a00b25 100644 --- a/classroom/snippets/src/main/java/ListCourses.java +++ b/classroom/snippets/src/main/java/ListCourses.java @@ -12,57 +12,55 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_list_courses] -import com.google.api.client.http.HttpRequestInitializer; +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Course; import com.google.api.services.classroom.model.ListCoursesResponse; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; - import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.ArrayList; -import java.util.Collections; +import java.util.Arrays; import java.util.List; /* Class to demonstrate the use of Classroom List Course API */ public class ListCourses { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + /** * Retrieves all courses with metadata * * @return list of courses with its metadata * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static List listCourses() throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSES)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static List listCourses() throws GeneralSecurityException, IOException { - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); String pageToken = null; List courses = new ArrayList<>(); try { do { - ListCoursesResponse response = service.courses().list() - .setPageSize(100) - .setPageToken(pageToken) - .execute(); + ListCoursesResponse response = + service.courses().list().setPageSize(100).setPageToken(pageToken).execute(); courses.addAll(response.getCourses()); pageToken = response.getNextPageToken(); } while (pageToken != null); @@ -82,4 +80,4 @@ public static List listCourses() throws IOException { return courses; } } -// [END classroom_list_courses] \ No newline at end of file +// [END classroom_list_courses] diff --git a/classroom/snippets/src/main/java/ListStudentSubmissions.java b/classroom/snippets/src/main/java/ListStudentSubmissions.java index c5d8b29f..20e322a1 100644 --- a/classroom/snippets/src/main/java/ListStudentSubmissions.java +++ b/classroom/snippets/src/main/java/ListStudentSubmissions.java @@ -14,24 +14,29 @@ // [START classroom_list_student_submissions_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.ListStudentSubmissionsResponse; import com.google.api.services.classroom.model.StudentSubmission; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.ArrayList; -import java.util.Collections; +import java.util.Arrays; import java.util.List; /* Class to demonstrate the use of Classroom List StudentSubmissions API. */ public class ListStudentSubmissions { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + /** * Retrieves a specific student's submissions for the specified course work. * @@ -40,26 +45,23 @@ public class ListStudentSubmissions { * @param userId - identifier of the student whose work to return. * @return - list of student submissions. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ public static List listStudentSubmissions( - String courseId, String courseWorkId, String userId) throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = - GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials); + String courseId, String courseWorkId, String userId) + throws GeneralSecurityException, IOException { // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); // [START classroom_list_student_submissions_code_snippet] - List studentSubmissions = new ArrayList<>(); String pageToken = null; @@ -90,6 +92,7 @@ public static List listStudentSubmissions( System.out.printf("Student submission: %s.\n", submission.getId()); } } + // [END classroom_list_student_submissions_code_snippet] } catch (GoogleJsonResponseException e) { // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); @@ -104,9 +107,73 @@ public static List listStudentSubmissions( throw e; } return studentSubmissions; + } - // [END classroom_list_student_submissions_code_snippet] + /** + * Lists all assigned grades for a particular coursework item. + * + * @param courseId - identifier of the course. + * @param courseWorkId - identifier of the course work. + * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. + */ + public static void listAssignedGrades(String courseId, String courseWorkId) + throws GeneralSecurityException, IOException { + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); + + List studentSubmissions = new ArrayList<>(); + String pageToken = null; + + try { + do { + // [START classroom_list_assigned_grades_code_snippet] + ListStudentSubmissionsResponse response = + service + .courses() + .courseWork() + .studentSubmissions() + .list(courseId, courseWorkId) + .setPageToken(pageToken) + .execute(); + + /* Ensure that the response is not null before retrieving data from it to avoid errors. */ + if (response.getStudentSubmissions() != null) { + studentSubmissions.addAll(response.getStudentSubmissions()); + pageToken = response.getNextPageToken(); + } + } while (pageToken != null); + + if (studentSubmissions.isEmpty()) { + System.out.println("No student submissions found."); + } else { + for (StudentSubmission submission : studentSubmissions) { + System.out.printf( + "User ID %s, Assigned grade: %s\n", + submission.getUserId(), submission.getAssignedGrade()); + } + } + // [END classroom_list_assigned_grades_code_snippet] + } catch (GoogleJsonResponseException e) { + // TODO (developer) - handle error appropriately + GoogleJsonError error = e.getDetails(); + if (error.getCode() == 404) { + System.out.printf( + "The courseId (%s) or courseWorkId (%s) does not exist.\n", courseId, courseWorkId); + } else { + throw e; + } + } catch (Exception e) { + throw e; + } } } // [END classroom_list_student_submissions_class] diff --git a/classroom/snippets/src/main/java/ListSubmissions.java b/classroom/snippets/src/main/java/ListSubmissions.java index d5356d70..105c9b41 100644 --- a/classroom/snippets/src/main/java/ListSubmissions.java +++ b/classroom/snippets/src/main/java/ListSubmissions.java @@ -14,24 +14,28 @@ // [START classroom_list_submissions_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.ListStudentSubmissionsResponse; import com.google.api.services.classroom.model.StudentSubmission; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.ArrayList; -import java.util.Collections; +import java.util.Arrays; import java.util.List; /* Class to demonstrate the use of Classroom List StudentSubmissions API. */ public class ListSubmissions { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); /** * Retrieves submissions for all students for the specified course work in a course. * @@ -39,21 +43,18 @@ public class ListSubmissions { * @param courseWorkId - identifier of the course work. * @return - list of student submissions. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ public static List listSubmissions(String courseId, String courseWorkId) - throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = - GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials); + throws GeneralSecurityException, IOException { // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); diff --git a/classroom/snippets/src/main/java/ListTopics.java b/classroom/snippets/src/main/java/ListTopics.java index 6412a1cb..ea35cf90 100644 --- a/classroom/snippets/src/main/java/ListTopics.java +++ b/classroom/snippets/src/main/java/ListTopics.java @@ -14,44 +14,47 @@ // [START classroom_list_topic_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.ListTopicResponse; import com.google.api.services.classroom.model.Topic; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.ArrayList; -import java.util.Collections; +import java.util.Arrays; import java.util.List; /* Class to demonstrate how to list topics in a course. */ public class ListTopics { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); + /** * List topics in a course. * * @param courseId - the id of the course to retrieve topics for. * @return - the list of topics in the course that the caller is permitted to view. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static List listTopics(String courseId) throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = - GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_TOPICS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials); + public static List listTopics(String courseId) + throws GeneralSecurityException, IOException { // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Classroom service = new Classroom.Builder( - new NetHttpTransport(), GsonFactory.getDefaultInstance(), requestInitializer) + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) .setApplicationName("Classroom samples") .build(); diff --git a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java index 252f51f5..d047ac2b 100644 --- a/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java +++ b/classroom/snippets/src/main/java/ModifyAttachmentsStudentSubmission.java @@ -12,12 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_modify_attachments_student_submissions_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; @@ -26,14 +25,19 @@ import com.google.api.services.classroom.model.Link; import com.google.api.services.classroom.model.ModifyAttachmentsRequest; import com.google.api.services.classroom.model.StudentSubmission; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; /* Class to demonstrate the use of Classroom ModifyAttachments StudentSubmissions API. */ public class ModifyAttachmentsStudentSubmission { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); + /** * Modify attachments on a student submission. * @@ -42,23 +46,20 @@ public class ModifyAttachmentsStudentSubmission { * @param id - identifier of the student submission. * @return - the modified student submission. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ public static StudentSubmission modifyAttachments(String courseId, String courseWorkId, String id) - throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_modify_attachments_student_submissions_code_snippet] @@ -67,28 +68,34 @@ public static StudentSubmission modifyAttachments(String courseId, String course // Create ModifyAttachmentRequest object that includes a new attachment with a link. Link link = new Link().setUrl("https://en.wikipedia.org/wiki/Irrational_number"); Attachment attachment = new Attachment().setLink(link); - ModifyAttachmentsRequest modifyAttachmentsRequest = new ModifyAttachmentsRequest() - .setAddAttachments(Arrays.asList(attachment)); + ModifyAttachmentsRequest modifyAttachmentsRequest = + new ModifyAttachmentsRequest().setAddAttachments(Arrays.asList(attachment)); // The modified studentSubmission object is returned with the new attachment added to it. - studentSubmission = service.courses().courseWork().studentSubmissions().modifyAttachments( - courseId, courseWorkId, id, modifyAttachmentsRequest) - .execute(); + studentSubmission = + service + .courses() + .courseWork() + .studentSubmissions() + .modifyAttachments(courseId, courseWorkId, id, modifyAttachmentsRequest) + .execute(); /* Prints the modified student submission. */ - System.out.printf("Modified student submission attachments: '%s'.\n", studentSubmission - .getAssignmentSubmission() - .getAttachments()); + System.out.printf( + "Modified student submission attachments: '%s'.\n", + studentSubmission.getAssignmentSubmission().getAttachments()); } catch (GoogleJsonResponseException e) { - //TODO (developer) - handle error appropriately + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { - System.out.printf("The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does " - + "not exist.\n", courseId, courseWorkId, id); + System.out.printf( + "The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does " + + "not exist.\n", + courseId, courseWorkId, id); } else { throw e; } - } catch(Exception e) { + } catch (Exception e) { throw e; } return studentSubmission; @@ -97,4 +104,4 @@ public static StudentSubmission modifyAttachments(String courseId, String course } } -// [END classroom_modify_attachments_student_submissions_class] \ No newline at end of file +// [END classroom_modify_attachments_student_submissions_class] diff --git a/classroom/snippets/src/main/java/PatchCourse.java b/classroom/snippets/src/main/java/PatchCourse.java index e8c5d31b..05bc71ac 100644 --- a/classroom/snippets/src/main/java/PatchCourse.java +++ b/classroom/snippets/src/main/java/PatchCourse.java @@ -12,54 +12,53 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_patch_course] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Course; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Patch Course API */ public class PatchCourse { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); + /** * Updates one or more fields in a course. * * @param courseId - Id of the course to update. * @return updated course * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static Course patchCourse(String courseId) throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSES)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static Course patchCourse(String courseId) throws GeneralSecurityException, IOException { + + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); Course course = null; try { - course = new Course() - .setSection("Period 3") - .setRoom("302"); - course = service.courses().patch(courseId, course) - .setUpdateMask("section,room") - .execute(); + course = new Course().setSection("Period 3").setRoom("302"); + course = service.courses().patch(courseId, course).setUpdateMask("section,room").execute(); System.out.printf("Course '%s' updated.\n", course.getName()); } catch (GoogleJsonResponseException e) { // TODO(developer) - handle error appropriately @@ -73,4 +72,4 @@ public static Course patchCourse(String courseId) throws IOException { return course; } } -// [END classroom_patch_course] \ No newline at end of file +// [END classroom_patch_course] diff --git a/classroom/snippets/src/main/java/PatchStudentSubmission.java b/classroom/snippets/src/main/java/PatchStudentSubmission.java index 1690ef8e..33ed5165 100644 --- a/classroom/snippets/src/main/java/PatchStudentSubmission.java +++ b/classroom/snippets/src/main/java/PatchStudentSubmission.java @@ -12,24 +12,28 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_patch_student_submissions_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.StudentSubmission; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Patch StudentSubmissions API. */ public class PatchStudentSubmission { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); /** * Updates the draft grade and/or assigned grade of a student submission. * @@ -38,51 +42,59 @@ public class PatchStudentSubmission { * @param id - identifier of the student submission. * @return - the updated student submission. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static StudentSubmission patchStudentSubmission(String courseId, String courseWorkId, - String id) throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static StudentSubmission patchStudentSubmission( + String courseId, String courseWorkId, String id) + throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_patch_student_submissions_code_snippet] StudentSubmission studentSubmission = null; try { // Updating the draftGrade and assignedGrade fields for the specific student submission. - StudentSubmission content = service.courses().courseWork().studentSubmissions() - .get(courseId, courseWorkId, id) - .execute(); + StudentSubmission content = + service + .courses() + .courseWork() + .studentSubmissions() + .get(courseId, courseWorkId, id) + .execute(); content.setAssignedGrade(90.00); content.setDraftGrade(80.00); // The updated studentSubmission object is returned with the new draftGrade and assignedGrade. - studentSubmission = service.courses().courseWork().studentSubmissions() - .patch(courseId, courseWorkId, id, content) - .set("updateMask", "draftGrade,assignedGrade") - .execute(); + studentSubmission = + service + .courses() + .courseWork() + .studentSubmissions() + .patch(courseId, courseWorkId, id, content) + .set("updateMask", "draftGrade,assignedGrade") + .execute(); /* Prints the updated student submission. */ - System.out.printf("Updated student submission draft grade (%s) and assigned grade (%s).\n", - studentSubmission.getDraftGrade(), - studentSubmission.getAssignedGrade()); + System.out.printf( + "Updated student submission draft grade (%s) and assigned grade (%s).\n", + studentSubmission.getDraftGrade(), studentSubmission.getAssignedGrade()); } catch (GoogleJsonResponseException e) { - //TODO (developer) - handle error appropriately + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { - System.out.printf("The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does " - + "not exist.\n", courseId, courseWorkId, id); + System.out.printf( + "The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does " + + "not exist.\n", + courseId, courseWorkId, id); } else { throw e; } @@ -95,4 +107,4 @@ public static StudentSubmission patchStudentSubmission(String courseId, String c } } -// [END classroom_patch_student_submissions_class] \ No newline at end of file +// [END classroom_patch_student_submissions_class] diff --git a/classroom/snippets/src/main/java/ReturnStudentSubmission.java b/classroom/snippets/src/main/java/ReturnStudentSubmission.java index 6b40fc96..61c891a0 100644 --- a/classroom/snippets/src/main/java/ReturnStudentSubmission.java +++ b/classroom/snippets/src/main/java/ReturnStudentSubmission.java @@ -12,60 +12,67 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_return_student_submissions_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Return StudentSubmissions API. */ public class ReturnStudentSubmission { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); /** - * Return a student submission back to the student which updates the submission state to `RETURNED`. + * Return a student submission back to the student which updates the submission state to + * `RETURNED`. * * @param courseId - identifier of the course. * @param courseWorkId - identifier of the course work. * @param id - identifier of the student submission. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ public static void returnSubmission(String courseId, String courseWorkId, String id) - throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSEWORK_STUDENTS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_return_student_submissions_code_snippet] try { - service.courses().courseWork().studentSubmissions() + service + .courses() + .courseWork() + .studentSubmissions() .classroomReturn(courseId, courseWorkId, id, null) .execute(); } catch (GoogleJsonResponseException e) { - //TODO (developer) - handle error appropriately + // TODO (developer) - handle error appropriately GoogleJsonError error = e.getDetails(); if (error.getCode() == 404) { - System.out.printf("The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does " - + "not exist.\n", courseId, courseWorkId, id); + System.out.printf( + "The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does " + + "not exist.\n", + courseId, courseWorkId, id); } else { throw e; } diff --git a/classroom/snippets/src/main/java/UpdateCourse.java b/classroom/snippets/src/main/java/UpdateCourse.java index 9e0d63ed..fdfd6062 100644 --- a/classroom/snippets/src/main/java/UpdateCourse.java +++ b/classroom/snippets/src/main/java/UpdateCourse.java @@ -12,46 +12,47 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_update_course] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Course; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate the use of Classroom Update Course API */ public class UpdateCourse { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES)); /** * Updates a course's metadata. * * @param courseId - Id of the course to update. * @return updated course * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static Course updateCourse(String courseId) throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSES)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); + public static Course updateCourse(String courseId) throws GeneralSecurityException, IOException { - // Create the classroom API client - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + // Create the classroom API client. + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); Course course = null; try { @@ -74,4 +75,4 @@ public static Course updateCourse(String courseId) throws IOException { return course; } } -// [END classroom_update_course] \ No newline at end of file +// [END classroom_update_course] diff --git a/classroom/snippets/src/main/java/UpdateTopic.java b/classroom/snippets/src/main/java/UpdateTopic.java index f45ffeca..e45c3cc8 100644 --- a/classroom/snippets/src/main/java/UpdateTopic.java +++ b/classroom/snippets/src/main/java/UpdateTopic.java @@ -12,24 +12,29 @@ // See the License for the specific language governing permissions and // limitations under the License. - // [START classroom_update_topic_class] +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.googleapis.json.GoogleJsonError; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.classroom.Classroom; import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Topic; -import com.google.auth.http.HttpCredentialsAdapter; -import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; -import java.util.Collections; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.Arrays; /* Class to demonstrate how to update one or more fields in a topic. */ public class UpdateTopic { + + /* Scopes required by this API call. If modifying these scopes, delete your previously saved + tokens/ folder. */ + static ArrayList SCOPES = + new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_TOPICS)); + /** * Update one or more fields in a topic in a course. * @@ -37,22 +42,19 @@ public class UpdateTopic { * @param topicId - the id of the topic to update. * @return - updated topic. * @throws IOException - if credentials file not found. + * @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created. */ - public static Topic updateTopic(String courseId, String topicId) throws IOException { - /* Load pre-authorized user credentials from the environment. - TODO(developer) - See https://developers.google.com/identity for - guides on implementing OAuth2 for your application. */ - GoogleCredentials credentials = GoogleCredentials.getApplicationDefault() - .createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_TOPICS)); - HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter( - credentials); - + public static Topic updateTopic(String courseId, String topicId) + throws GeneralSecurityException, IOException { // Create the classroom API client. - Classroom service = new Classroom.Builder(new NetHttpTransport(), - GsonFactory.getDefaultInstance(), - requestInitializer) - .setApplicationName("Classroom samples") - .build(); + final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); + Classroom service = + new Classroom.Builder( + HTTP_TRANSPORT, + GsonFactory.getDefaultInstance(), + ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES)) + .setApplicationName("Classroom samples") + .build(); // [START classroom_update_topic_code_snippet] @@ -65,10 +67,14 @@ public static Topic updateTopic(String courseId, String topicId) throws IOExcept topicToUpdate.setName("Semester 2"); /* Call the patch endpoint and set the updateMask query parameter to the field that needs to - be updated. */ - topic = service.courses().topics().patch(courseId, topicId, topicToUpdate) - .set("updateMask", "name") - .execute(); + be updated. */ + topic = + service + .courses() + .topics() + .patch(courseId, topicId, topicToUpdate) + .set("updateMask", "name") + .execute(); /* Prints the updated topic. */ System.out.printf("Topic '%s' updated.\n", topic.getName()); diff --git a/classroom/snippets/src/test/java/BaseTest.java b/classroom/snippets/src/test/java/BaseTest.java index b0c5286e..1b2eb04d 100644 --- a/classroom/snippets/src/test/java/BaseTest.java +++ b/classroom/snippets/src/test/java/BaseTest.java @@ -19,13 +19,11 @@ import com.google.api.services.classroom.ClassroomScopes; import com.google.api.services.classroom.model.Course; import com.google.api.services.classroom.model.CourseAlias; -import java.util.Collections; -import java.util.List; -import org.junit.After; -import org.junit.Before; - import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; import java.util.UUID; +import org.junit.After; // Base class for integration tests. public class BaseTest { @@ -38,10 +36,11 @@ public class BaseTest { * @return an authorized Classroom client service * @throws IOException - if credentials file not found. */ - protected Classroom buildService() throws Exception { + protected Classroom buildService(ArrayList SCOPES) + throws GeneralSecurityException, IOException { /* Scopes required by this API call. If modifying these scopes, delete your previously saved tokens/ folder. */ - final List SCOPES = Collections.singletonList(ClassroomScopes.CLASSROOM_COURSES); + SCOPES.add(ClassroomScopes.CLASSROOM_COURSES); // Create the classroom API client final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); @@ -54,16 +53,17 @@ protected Classroom buildService() throws Exception { .build(); } - @Before - public void setup() throws Exception { - this.service = buildService(); + public void setup(ArrayList scopes) throws GeneralSecurityException, IOException { + this.service = buildService(scopes); this.testCourse = CreateCourse.createCourse(); createAlias(this.testCourse.getId()); } @After public void tearDown() throws IOException { - deleteCourse(this.testCourse.getId()); + if (this.testCourse != null) { + deleteCourse(this.testCourse.getId()); + } this.testCourse = null; } diff --git a/classroom/snippets/src/test/java/TestAcceptInvitation.java b/classroom/snippets/src/test/java/TestAcceptInvitation.java new file mode 100644 index 00000000..98a6fec3 --- /dev/null +++ b/classroom/snippets/src/test/java/TestAcceptInvitation.java @@ -0,0 +1,39 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import java.io.IOException; +import java.security.GeneralSecurityException; +import org.junit.Assert; +import org.junit.Test; + +public class TestAcceptInvitation { + + private String invitationId = "insert_invitation_id"; + + @Test + public void testAcceptInvitation() throws GeneralSecurityException, IOException { + AcceptInvitation.acceptInvitation(invitationId); + /* Once an invitation is accepted, it is removed and the user is added to the course as a + teacher or a student. */ + Assert.assertThrows( + GoogleJsonResponseException.class, () -> GetInvitation.getInvitation(invitationId)); + } + + @Test + public void testAcceptInvitationWithInvalidId() { + Assert.assertThrows( + GoogleJsonResponseException.class, () -> AcceptInvitation.acceptInvitation("invalid-id")); + } +} diff --git a/classroom/snippets/src/test/java/TestAddAliasToCourse.java b/classroom/snippets/src/test/java/TestAddAliasToCourse.java index c5b85777..4eb659eb 100644 --- a/classroom/snippets/src/test/java/TestAddAliasToCourse.java +++ b/classroom/snippets/src/test/java/TestAddAliasToCourse.java @@ -14,6 +14,7 @@ import com.google.api.services.classroom.model.CourseAlias; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.List; import org.junit.Assert; import org.junit.Test; @@ -22,13 +23,13 @@ public class TestAddAliasToCourse extends BaseTest { @Test - public void testAddCourseAlias() throws IOException { + public void testAddCourseAlias() throws GeneralSecurityException, IOException { + // Include the scopes required to run the code example for testing purposes. + setup(AddAliasToCourse.SCOPES); CourseAlias courseAlias = AddAliasToCourse.addAliasToCourse(testCourse.getId()); - List courseAliases = service.courses().aliases() - .list(testCourse.getId() - ).execute() - .getAliases(); + List courseAliases = + service.courses().aliases().list(testCourse.getId()).execute().getAliases(); Assert.assertNotNull("Course alias not returned.", courseAlias); Assert.assertTrue("No course aliases exist.", courseAliases.size() > 0); } -} \ No newline at end of file +} diff --git a/classroom/snippets/src/test/java/TestAddStudent.java b/classroom/snippets/src/test/java/TestAddStudent.java index f023989a..2b185c9a 100644 --- a/classroom/snippets/src/test/java/TestAddStudent.java +++ b/classroom/snippets/src/test/java/TestAddStudent.java @@ -12,22 +12,26 @@ // See the License for the specific language governing permissions and // limitations under the License. -import com.google.api.services.classroom.model.Course; import com.google.api.services.classroom.model.Student; +import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; -import java.io.IOException; // Unit test class for Add Student classroom snippet public class TestAddStudent extends BaseTest { + private String studentId = "insert_student_id"; + @Test - public void testAddStudent() throws IOException { - Course course = CreateCourse.createCourse(); - Student student = AddStudent.addStudent(course.getId(), course.getEnrollmentCode()); - deleteCourse(course.getId()); + public void testAddStudent() throws GeneralSecurityException, IOException { + // Include the scopes required to run the code example for testing purposes. + setup(AddStudent.SCOPES); + Student student = + AddStudent.addStudent(testCourse.getId(), testCourse.getEnrollmentCode(), this.studentId); Assert.assertNotNull("Student not returned.", student); - Assert.assertEquals("Student added to wrong course.", course.getId(), - student.getCourseId()); + Assert.assertNotNull("Course not returned.", student.getCourseId()); + Assert.assertEquals( + "Student added to wrong course.", testCourse.getId(), student.getCourseId()); } -} \ No newline at end of file +} diff --git a/classroom/snippets/src/test/java/TestAddTeacher.java b/classroom/snippets/src/test/java/TestAddTeacher.java index f2846ddf..58eaae57 100644 --- a/classroom/snippets/src/test/java/TestAddTeacher.java +++ b/classroom/snippets/src/test/java/TestAddTeacher.java @@ -13,19 +13,23 @@ // limitations under the License. import com.google.api.services.classroom.model.Teacher; +import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; -import java.io.IOException; // Unit test class for Add Teacher classroom snippet public class TestAddTeacher extends BaseTest { - private String otherUser = "gduser1@workspacesamples.dev"; + + private String teacherEmail = "insert_teacher_email"; @Test - public void testAddTeacher() throws IOException { - Teacher teacher = AddTeacher.addTeacher(testCourse.getId(), this.otherUser); + public void testAddTeacher() throws GeneralSecurityException, IOException { + // Include the scopes required to run the code example for testing purposes. + setup(AddTeacher.SCOPES); + Teacher teacher = AddTeacher.addTeacher(testCourse.getId(), this.teacherEmail); Assert.assertNotNull("Teacher not returned.", teacher); - Assert.assertEquals("Teacher added to wrong course.", testCourse.getId(), - teacher.getCourseId()); + Assert.assertEquals( + "Teacher added to wrong course.", testCourse.getId(), teacher.getCourseId()); } -} \ No newline at end of file +} diff --git a/classroom/snippets/src/test/java/TestBatchAddStudents.java b/classroom/snippets/src/test/java/TestBatchAddStudents.java index 04ee930e..1f66874a 100644 --- a/classroom/snippets/src/test/java/TestBatchAddStudents.java +++ b/classroom/snippets/src/test/java/TestBatchAddStudents.java @@ -12,18 +12,19 @@ // See the License for the specific language governing permissions and // limitations under the License. -import org.junit.Test; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.Arrays; import java.util.List; +import org.junit.Test; // Unit test class for Batch Add Students classroom snippet public class TestBatchAddStudents extends BaseTest { @Test - public void testBatchAddStudents() throws IOException { - List studentEmails = Arrays.asList("gduser2@workpsacesamples.dev", - "gduser3@workpsacesamples.dev"); + public void testBatchAddStudents() throws GeneralSecurityException, IOException { + setup(BatchAddStudents.SCOPES); + List studentEmails = Arrays.asList("insert_student_1_email", "insert_student_2_email"); BatchAddStudents.batchAddStudents(testCourse.getId(), studentEmails); } -} \ No newline at end of file +} diff --git a/classroom/snippets/src/test/java/TestCreateCourse.java b/classroom/snippets/src/test/java/TestCreateCourse.java index 381f2c2c..a251f15c 100644 --- a/classroom/snippets/src/test/java/TestCreateCourse.java +++ b/classroom/snippets/src/test/java/TestCreateCourse.java @@ -13,15 +13,18 @@ // limitations under the License. import com.google.api.services.classroom.model.Course; +import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; -import java.io.IOException; // Unit test class for Create Course classroom snippet public class TestCreateCourse extends BaseTest { @Test - public void testCreateCourse() throws IOException { + public void testCreateCourse() throws GeneralSecurityException, IOException { + // Include the scopes required to run the code example for testing purposes. + setup(CreateCourse.SCOPES); Course course = CreateCourse.createCourse(); Assert.assertNotNull("Course not returned.", course); deleteCourse(course.getId()); diff --git a/classroom/snippets/src/test/java/TestCreateCourseWithAlias.java b/classroom/snippets/src/test/java/TestCreateCourseWithAlias.java index 5152b984..2d906fbf 100644 --- a/classroom/snippets/src/test/java/TestCreateCourseWithAlias.java +++ b/classroom/snippets/src/test/java/TestCreateCourseWithAlias.java @@ -15,6 +15,7 @@ import com.google.api.services.classroom.model.Course; import com.google.api.services.classroom.model.CourseAlias; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.List; import org.junit.Assert; import org.junit.Test; @@ -23,14 +24,13 @@ public class TestCreateCourseWithAlias extends BaseTest { @Test - public void testCreateCourseWithAlias() throws IOException { + public void testCreateCourseWithAlias() throws GeneralSecurityException, IOException { + setup(CreateCourseWithAlias.SCOPES); Course course = CreateCourseWithAlias.createCourseWithAlias(); - List courseAliases = service.courses().aliases() - .list(course.getId() - ).execute() - .getAliases(); + List courseAliases = + service.courses().aliases().list(course.getId()).execute().getAliases(); Assert.assertNotNull("Course not returned.", course); Assert.assertTrue("No course aliases exist.", courseAliases.size() > 0); deleteCourse(course.getId()); } -} \ No newline at end of file +} diff --git a/classroom/snippets/src/test/java/TestCreateCourseWork.java b/classroom/snippets/src/test/java/TestCreateCourseWork.java index e3a25b6b..5a160af0 100644 --- a/classroom/snippets/src/test/java/TestCreateCourseWork.java +++ b/classroom/snippets/src/test/java/TestCreateCourseWork.java @@ -14,6 +14,7 @@ import com.google.api.services.classroom.model.CourseWork; import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; @@ -21,7 +22,9 @@ public class TestCreateCourseWork extends BaseTest { @Test - public void testCreateCoursework() throws IOException { + public void testCreateCoursework() throws GeneralSecurityException, IOException { + // Include the scopes required to run the code example for testing purposes. + setup(CreateCourseWork.SCOPES); CourseWork courseWork = CreateCourseWork.createCourseWork(testCourse.getId()); Assert.assertNotNull("Coursework not returned.", courseWork); } diff --git a/classroom/snippets/src/test/java/TestCreateInvitation.java b/classroom/snippets/src/test/java/TestCreateInvitation.java new file mode 100644 index 00000000..13095342 --- /dev/null +++ b/classroom/snippets/src/test/java/TestCreateInvitation.java @@ -0,0 +1,43 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.services.classroom.model.Invitation; +import java.io.IOException; +import java.security.GeneralSecurityException; +import org.junit.Assert; +import org.junit.Test; + +public class TestCreateInvitation extends BaseTest { + + private String userId = "insert_user_id"; + + @Test + public void testCreateInvitation() throws GeneralSecurityException, IOException { + setup(CreateInvitation.SCOPES); + Invitation invitation = CreateInvitation.createInvitation(testCourse.getId(), userId); + Assert.assertNotNull("Invitation not created", invitation); + Assert.assertEquals(invitation.getCourseId(), testCourse.getId()); + Assert.assertEquals(invitation.getUserId(), userId); + } + + @Test + public void testCreateInvitationWithInvalidCourseId() + throws GeneralSecurityException, IOException { + setup(CreateInvitation.SCOPES); + Assert.assertThrows( + GoogleJsonResponseException.class, + () -> CreateInvitation.createInvitation("invalid-course-id", userId)); + } +} diff --git a/classroom/snippets/src/test/java/TestCreateTopic.java b/classroom/snippets/src/test/java/TestCreateTopic.java index e2d77586..381921d9 100644 --- a/classroom/snippets/src/test/java/TestCreateTopic.java +++ b/classroom/snippets/src/test/java/TestCreateTopic.java @@ -14,6 +14,7 @@ import com.google.api.services.classroom.model.Topic; import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; @@ -21,7 +22,8 @@ public class TestCreateTopic extends BaseTest { @Test - public void testCreateTopic() throws IOException { + public void testCreateTopic() throws GeneralSecurityException, IOException { + setup(CreateTopic.SCOPES); Topic topic = CreateTopic.createTopic(testCourse.getId()); Assert.assertNotNull("Topic not returned.", topic); } diff --git a/classroom/snippets/src/test/java/TestDeleteInvitation.java b/classroom/snippets/src/test/java/TestDeleteInvitation.java new file mode 100644 index 00000000..84448189 --- /dev/null +++ b/classroom/snippets/src/test/java/TestDeleteInvitation.java @@ -0,0 +1,42 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import com.google.api.client.googleapis.json.GoogleJsonResponseException; +import com.google.api.services.classroom.model.Invitation; +import java.io.IOException; +import java.security.GeneralSecurityException; +import org.junit.Assert; +import org.junit.Test; + +public class TestDeleteInvitation extends BaseTest { + + private String userId = "insert_user_id"; + + @Test + public void testDeleteInvitation() throws GeneralSecurityException, IOException { + setup(DeleteInvitation.SCOPES); + Invitation invitation = CreateInvitation.createInvitation(testCourse.getId(), userId); + DeleteInvitation.deleteInvitation(invitation.getId()); + Assert.assertThrows( + GoogleJsonResponseException.class, () -> GetInvitation.getInvitation(invitation.getId())); + } + + @Test + public void testDeleteInvitationWithInvalidId() throws GeneralSecurityException, IOException { + setup(DeleteInvitation.SCOPES); + Assert.assertThrows( + GoogleJsonResponseException.class, + () -> DeleteInvitation.deleteInvitation("invalid-invitation-id")); + } +} diff --git a/classroom/snippets/src/test/java/TestDeleteTopic.java b/classroom/snippets/src/test/java/TestDeleteTopic.java index b093865c..bffff20d 100644 --- a/classroom/snippets/src/test/java/TestDeleteTopic.java +++ b/classroom/snippets/src/test/java/TestDeleteTopic.java @@ -15,15 +15,19 @@ import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.services.classroom.model.Topic; import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; public class TestDeleteTopic extends BaseTest { + @Test - public void testDeleteTopic() throws IOException { + public void testDeleteTopic() throws GeneralSecurityException, IOException { + setup(DeleteTopic.SCOPES); Topic topic = CreateTopic.createTopic(testCourse.getId()); DeleteTopic.deleteTopic(testCourse.getId(), topic.getTopicId()); - Assert.assertThrows(GoogleJsonResponseException.class, + Assert.assertThrows( + GoogleJsonResponseException.class, () -> GetTopic.getTopic(testCourse.getId(), topic.getTopicId())); } } diff --git a/classroom/snippets/src/test/java/TestGetCourse.java b/classroom/snippets/src/test/java/TestGetCourse.java index c43059f7..ac95bda5 100644 --- a/classroom/snippets/src/test/java/TestGetCourse.java +++ b/classroom/snippets/src/test/java/TestGetCourse.java @@ -13,15 +13,17 @@ // limitations under the License. import com.google.api.services.classroom.model.Course; +import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; -import java.io.IOException; // Unit test class for Get Course classroom snippet public class TestGetCourse extends BaseTest { @Test - public void testGetCourse() throws IOException { + public void testGetCourse() throws GeneralSecurityException, IOException { + setup(GetCourse.SCOPES); Course course = GetCourse.getCourse(testCourse.getId()); Assert.assertNotNull("Course not returned.", course); Assert.assertEquals("Wrong course returned.", testCourse.getId(), course.getId()); diff --git a/classroom/snippets/src/test/java/TestGetTopic.java b/classroom/snippets/src/test/java/TestGetTopic.java index b7cb7840..39775ff2 100644 --- a/classroom/snippets/src/test/java/TestGetTopic.java +++ b/classroom/snippets/src/test/java/TestGetTopic.java @@ -14,6 +14,7 @@ import com.google.api.services.classroom.model.Topic; import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; @@ -21,7 +22,8 @@ public class TestGetTopic extends BaseTest { @Test - public void testGetTopic() throws IOException { + public void testGetTopic() throws GeneralSecurityException, IOException { + setup(GetTopic.SCOPES); Topic topic = CreateTopic.createTopic(testCourse.getId()); Topic getTopic = GetTopic.getTopic(testCourse.getId(), topic.getTopicId()); Assert.assertNotNull("Topic could not be created.", topic); diff --git a/classroom/snippets/src/test/java/TestListCourseAliases.java b/classroom/snippets/src/test/java/TestListCourseAliases.java index f18c1cce..d3e357b5 100644 --- a/classroom/snippets/src/test/java/TestListCourseAliases.java +++ b/classroom/snippets/src/test/java/TestListCourseAliases.java @@ -13,17 +13,19 @@ // limitations under the License. import com.google.api.services.classroom.model.CourseAlias; -import org.junit.Assert; -import org.junit.Test; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.List; +import org.junit.Assert; +import org.junit.Test; // Unit test class for List Course Aliases classroom snippet public class TestListCourseAliases extends BaseTest { @Test - public void testListCourseAliases() throws IOException { + public void testListCourseAliases() throws GeneralSecurityException, IOException { + setup(ListCourseAliases.SCOPES); List courseAliases = ListCourseAliases.listCourseAliases(testCourse.getId()); Assert.assertTrue("Incorrect number of course aliases returned.", courseAliases.size() == 1); } -} \ No newline at end of file +} diff --git a/classroom/snippets/src/test/java/TestListCourses.java b/classroom/snippets/src/test/java/TestListCourses.java index fe254b76..7a4dd553 100644 --- a/classroom/snippets/src/test/java/TestListCourses.java +++ b/classroom/snippets/src/test/java/TestListCourses.java @@ -13,16 +13,17 @@ // limitations under the License. import com.google.api.services.classroom.model.Course; -import org.junit.Assert; -import org.junit.Test; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.List; +import org.junit.Assert; +import org.junit.Test; // Unit test class for List Course classroom snippet public class TestListCourses { @Test - public void testListCourses() throws IOException { + public void testListCourses() throws GeneralSecurityException, IOException { List courses = ListCourses.listCourses(); Assert.assertTrue("No courses returned.", courses.size() > 0); } diff --git a/classroom/snippets/src/test/java/TestListSubmissions.java b/classroom/snippets/src/test/java/TestListSubmissions.java index 148e9529..1649e926 100644 --- a/classroom/snippets/src/test/java/TestListSubmissions.java +++ b/classroom/snippets/src/test/java/TestListSubmissions.java @@ -14,6 +14,7 @@ import com.google.api.services.classroom.model.StudentSubmission; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.List; import org.junit.Assert; import org.junit.Test; @@ -22,10 +23,9 @@ public class TestListSubmissions extends BaseTest { @Test - public void testListSubmissions() throws IOException { - List submissions = ListSubmissions.listSubmissions( - testCourse.getId(), - "-"); + public void testListSubmissions() throws GeneralSecurityException, IOException { + setup(ListSubmissions.SCOPES); + List submissions = ListSubmissions.listSubmissions(testCourse.getId(), "-"); Assert.assertNotNull("No submissions returned.", submissions); } } diff --git a/classroom/snippets/src/test/java/TestListTopics.java b/classroom/snippets/src/test/java/TestListTopics.java index 081be57b..f0b0bc85 100644 --- a/classroom/snippets/src/test/java/TestListTopics.java +++ b/classroom/snippets/src/test/java/TestListTopics.java @@ -14,6 +14,7 @@ import com.google.api.services.classroom.model.Topic; import java.io.IOException; +import java.security.GeneralSecurityException; import java.util.List; import org.junit.Assert; import org.junit.Test; @@ -22,7 +23,8 @@ public class TestListTopics extends BaseTest { @Test - public void testListTopics() throws IOException { + public void testListTopics() throws GeneralSecurityException, IOException { + setup(ListTopics.SCOPES); CreateTopic.createTopic(testCourse.getId()); List listTopics = ListTopics.listTopics(testCourse.getId()); Assert.assertNotNull("Topics could not be retrieved.", listTopics); diff --git a/classroom/snippets/src/test/java/TestPatchCourse.java b/classroom/snippets/src/test/java/TestPatchCourse.java index 51e05a43..a7d0d92f 100644 --- a/classroom/snippets/src/test/java/TestPatchCourse.java +++ b/classroom/snippets/src/test/java/TestPatchCourse.java @@ -13,15 +13,17 @@ // limitations under the License. import com.google.api.services.classroom.model.Course; +import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; -import java.io.IOException; // Unit test class for Patch Course classroom snippet public class TestPatchCourse extends BaseTest { @Test - public void testPatchCourse() throws IOException { + public void testPatchCourse() throws GeneralSecurityException, IOException { + setup(PatchCourse.SCOPES); Course course = PatchCourse.patchCourse(testCourse.getId()); Assert.assertNotNull("Course not returned.", course); Assert.assertEquals("Wrong course returned.", testCourse.getId(), course.getId()); diff --git a/classroom/snippets/src/test/java/TestUpdateCourse.java b/classroom/snippets/src/test/java/TestUpdateCourse.java index f5e0080d..3c7cbda2 100644 --- a/classroom/snippets/src/test/java/TestUpdateCourse.java +++ b/classroom/snippets/src/test/java/TestUpdateCourse.java @@ -13,15 +13,17 @@ // limitations under the License. import com.google.api.services.classroom.model.Course; +import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; -import java.io.IOException; // Unit test class for Update Course classroom snippet public class TestUpdateCourse extends BaseTest { @Test - public void testUpdateCourse() throws IOException { + public void testUpdateCourse() throws GeneralSecurityException, IOException { + setup(UpdateCourse.SCOPES); Course course = UpdateCourse.updateCourse(testCourse.getId()); Assert.assertNotNull("Course not returned.", course); Assert.assertEquals("Wrong course returned.", testCourse.getId(), course.getId()); diff --git a/classroom/snippets/src/test/java/TestUpdateTopic.java b/classroom/snippets/src/test/java/TestUpdateTopic.java index 5b1b9eec..3144ff2d 100644 --- a/classroom/snippets/src/test/java/TestUpdateTopic.java +++ b/classroom/snippets/src/test/java/TestUpdateTopic.java @@ -11,15 +11,19 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + import com.google.api.services.classroom.model.Topic; import java.io.IOException; +import java.security.GeneralSecurityException; import org.junit.Assert; import org.junit.Test; // Unit test class for Update Topic classroom snippet public class TestUpdateTopic extends BaseTest { + @Test - public void testUpdateTopic() throws IOException { + public void testUpdateTopic() throws GeneralSecurityException, IOException { + setup(UpdateTopic.SCOPES); Topic topic = CreateTopic.createTopic(testCourse.getId()); System.out.println("New topic created: " + topic.getName()); Topic updatedTopic = UpdateTopic.updateTopic(testCourse.getId(), topic.getTopicId());