Skip to content

Commit 079711b

Browse files
feat: updating authorization flow & added invitation resource Classroom code snippets. (googleworkspace#601)
* add alias snippet and unit test * java snippets for topics, create coursework, create alias * updates based on team review * renamed alias methods, added devsite comments * added missing license header to TestDeleteTopic.java * Adding List, ModifyAttachments, Patch, Return StudentSubmission methods * minor fixes * adding comments for developer * modifications based on team review * modifications based on team review * Adding print statements and devsite tag to ListCourseAliases * added class templates for guardians and guardianInvitations * guardian and guardian invitation implementation * tests for list guardians and list guardian invitations * adding tests for guardians and guardian invites. removing use of ADC for auth. * base test updates * updates based on team feedback * fixing BaseTest.java * updating copyright year to 2023 for new files * testing google-java-format plugin to fix lint errors * updated files after running google-java-format plugin * updated BaseTest * fixing copyright header for BaseTest * updated auth for AddAlias and CreateCourse classes * removing unused imports * Fixing comment. * specifying errors in method signatures * updated BaseTest and AddStudent * Adding annotation for GeneralSecurityException * updating AddStudent method signature and test * updated AddTeacher and AddStudent test * updated auth for BatchAddStudents * updated auth for CreateCourseWork * updated auth for Topic related classes and tests * added link to CourseState info and updated GetTopic error handling * updated auth for remaining classes * updated auth for CreateCourseWithAlias * CreateInvitation code snippet * accept, create, delete invitation classes * list invitations class * listAssignedGrades method in ListStudentSubmissions class * deleting ListInvitations classes until bug is fixed * team review updates * google java format for tests * google-java-format on classes * google java format DeleteTopic * added test cases to CreateInvitation and DeleteInvitation classes * additional comment in accept invitation test case * fixing java formatting * google java format fix
1 parent 8718e29 commit 079711b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1200
-594
lines changed
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// [START classroom_accept_invitation]
16+
17+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
18+
import com.google.api.client.googleapis.json.GoogleJsonError;
19+
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
20+
import com.google.api.client.http.javanet.NetHttpTransport;
21+
import com.google.api.client.json.gson.GsonFactory;
22+
import com.google.api.services.classroom.Classroom;
23+
import com.google.api.services.classroom.ClassroomScopes;
24+
import java.io.IOException;
25+
import java.security.GeneralSecurityException;
26+
import java.util.ArrayList;
27+
import java.util.Arrays;
28+
29+
/* Class to demonstrate the use of Classroom Accept Invitation API. */
30+
public class AcceptInvitation {
31+
32+
/* Scopes required by this API call. If modifying these scopes, delete your previously saved
33+
tokens/ folder. */
34+
static ArrayList<String> SCOPES =
35+
new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS));
36+
37+
/**
38+
* Accepts an invitation to a course.
39+
*
40+
* @param id - the identifier of the invitation to accept.
41+
* @throws IOException - if credentials file not found.
42+
* @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created.
43+
*/
44+
public static void acceptInvitation(String id) throws GeneralSecurityException, IOException {
45+
46+
// Create the classroom API client.
47+
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
48+
Classroom service =
49+
new Classroom.Builder(
50+
HTTP_TRANSPORT,
51+
GsonFactory.getDefaultInstance(),
52+
ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES))
53+
.setApplicationName("Classroom samples")
54+
.build();
55+
56+
// [START classroom_accept_invitation_code_snippet]
57+
try {
58+
service.invitations().accept(id).execute();
59+
System.out.printf("Invitation (%s) was accepted.\n", id);
60+
} catch (GoogleJsonResponseException e) {
61+
GoogleJsonError error = e.getDetails();
62+
if (error.getCode() == 404) {
63+
System.out.printf("The invitation id (%s) does not exist.\n", id);
64+
}
65+
throw e;
66+
} catch (Exception e) {
67+
throw e;
68+
}
69+
// [END classroom_accept_invitation_code_snippet]
70+
}
71+
}
72+
// [END classroom_accept_invitation]

Diff for: classroom/snippets/src/main/java/AddAliasToCourse.java

+23-23
Original file line numberDiff line numberDiff line change
@@ -12,61 +12,61 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
1615
// [START classroom_add_alias_to_course_class]
1716

17+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
1818
import com.google.api.client.googleapis.json.GoogleJsonError;
1919
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
20-
import com.google.api.client.http.HttpRequestInitializer;
2120
import com.google.api.client.http.javanet.NetHttpTransport;
2221
import com.google.api.client.json.gson.GsonFactory;
2322
import com.google.api.services.classroom.Classroom;
2423
import com.google.api.services.classroom.ClassroomScopes;
2524
import com.google.api.services.classroom.model.CourseAlias;
26-
import com.google.auth.http.HttpCredentialsAdapter;
27-
import com.google.auth.oauth2.GoogleCredentials;
2825
import java.io.IOException;
29-
import java.util.Collections;
26+
import java.security.GeneralSecurityException;
27+
import java.util.ArrayList;
28+
import java.util.Arrays;
3029

3130
/* Class to demonstrate the use of Classroom Create Alias API. */
3231
public class AddAliasToCourse {
32+
/* Scopes required by this API call. If modifying these scopes, delete your previously saved
33+
tokens/ folder. */
34+
static ArrayList<String> SCOPES =
35+
new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_COURSES));
36+
3337
/**
3438
* Add an alias on an existing course.
3539
*
3640
* @param courseId - id of the course to add an alias to.
3741
* @return - newly created course alias.
3842
* @throws IOException - if credentials file not found.
43+
* @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created.
3944
*/
40-
public static CourseAlias addAliasToCourse(String courseId) throws IOException {
41-
/* Load pre-authorized user credentials from the environment.
42-
TODO(developer) - See https://developers.google.com/identity for
43-
guides on implementing OAuth2 for your application. */
44-
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
45-
.createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_COURSES));
46-
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
47-
credentials);
45+
public static CourseAlias addAliasToCourse(String courseId)
46+
throws GeneralSecurityException, IOException {
4847

4948
// Create the classroom API client.
50-
Classroom service = new Classroom.Builder(new NetHttpTransport(),
51-
GsonFactory.getDefaultInstance(),
52-
requestInitializer)
53-
.setApplicationName("Classroom samples")
54-
.build();
49+
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
50+
Classroom service =
51+
new Classroom.Builder(
52+
HTTP_TRANSPORT,
53+
GsonFactory.getDefaultInstance(),
54+
ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES))
55+
.setApplicationName("Classroom samples")
56+
.build();
5557

5658
// [START classroom_add_alias_to_course_code_snippet]
5759

5860
/* Create a new CourseAlias object with a project-wide alias. Project-wide aliases use a prefix
5961
of "p:" and can only be seen and used by the application that created them. */
60-
CourseAlias content = new CourseAlias()
61-
.setAlias("p:biology_10");
62+
CourseAlias content = new CourseAlias().setAlias("p:biology_10");
6263
CourseAlias courseAlias = null;
6364

6465
try {
65-
courseAlias = service.courses().aliases().create(courseId, content)
66-
.execute();
66+
courseAlias = service.courses().aliases().create(courseId, content).execute();
6767
System.out.printf("Course alias created: %s \n", courseAlias.getAlias());
6868
} catch (GoogleJsonResponseException e) {
69-
//TODO (developer) - handle error appropriately
69+
// TODO (developer) - handle error appropriately
7070
GoogleJsonError error = e.getDetails();
7171
if (error.getCode() == 409) {
7272
System.out.printf("The course alias already exists: %s.\n", content);

Diff for: classroom/snippets/src/main/java/AddStudent.java

+34-27
Original file line numberDiff line numberDiff line change
@@ -12,57 +12,64 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
1615
// [START classroom_add_student]
1716

17+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
1818
import com.google.api.client.googleapis.json.GoogleJsonError;
1919
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
20-
import com.google.api.client.http.HttpRequestInitializer;
2120
import com.google.api.client.http.javanet.NetHttpTransport;
2221
import com.google.api.client.json.gson.GsonFactory;
2322
import com.google.api.services.classroom.Classroom;
2423
import com.google.api.services.classroom.ClassroomScopes;
2524
import com.google.api.services.classroom.model.Student;
26-
import com.google.auth.http.HttpCredentialsAdapter;
27-
import com.google.auth.oauth2.GoogleCredentials;
2825
import java.io.IOException;
29-
import java.util.Collections;
26+
import java.security.GeneralSecurityException;
27+
import java.util.ArrayList;
28+
import java.util.Arrays;
3029

3130
/* Class to demonstrate the use of Classroom Add Student API */
3231
public class AddStudent {
32+
33+
/* Scopes required by this API call. If modifying these scopes, delete your previously saved
34+
tokens/ folder. */
35+
static ArrayList<String> SCOPES =
36+
new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS));
37+
3338
/**
3439
* Add a student in a specified course.
3540
*
36-
* @param courseId - Id of the course.
41+
* @param courseId - Id of the course.
3742
* @param enrollmentCode - Code of the course to enroll.
3843
* @return newly added student
3944
* @throws IOException - if credentials file not found.
45+
* @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created.
4046
*/
41-
public static Student addStudent(String courseId, String enrollmentCode)
42-
throws IOException {
43-
/* Load pre-authorized user credentials from the environment.
44-
TODO(developer) - See https://developers.google.com/identity for
45-
guides on implementing OAuth2 for your application. */
46-
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
47-
.createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_ROSTERS));
48-
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
49-
credentials);
47+
public static Student addStudent(String courseId, String enrollmentCode, String studentId)
48+
throws GeneralSecurityException, IOException {
5049

51-
// Create the classroom API client
52-
Classroom service = new Classroom.Builder(new NetHttpTransport(),
53-
GsonFactory.getDefaultInstance(),
54-
requestInitializer)
55-
.setApplicationName("Classroom samples")
56-
.build();
50+
// Create the classroom API client.
51+
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
52+
Classroom service =
53+
new Classroom.Builder(
54+
HTTP_TRANSPORT,
55+
GsonFactory.getDefaultInstance(),
56+
ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES))
57+
.setApplicationName("Classroom samples")
58+
.build();
5759

58-
Student student = new Student().setUserId("[email protected]");
60+
Student student = new Student().setUserId(studentId);
5961
try {
6062
// Enrolling a student to a specified course
61-
student = service.courses().students().create(courseId, student)
62-
.setEnrollmentCode(enrollmentCode)
63-
.execute();
63+
student =
64+
service
65+
.courses()
66+
.students()
67+
.create(courseId, student)
68+
.setEnrollmentCode(enrollmentCode)
69+
.execute();
6470
// Prints the course id with the Student name
65-
System.out.printf("User '%s' was enrolled as a student in the course with ID '%s'.\n",
71+
System.out.printf(
72+
"User '%s' was enrolled as a student in the course with ID '%s'.\n",
6673
student.getProfile().getName().getFullName(), courseId);
6774
} catch (GoogleJsonResponseException e) {
6875
// TODO(developer) - handle error appropriately
@@ -78,4 +85,4 @@ public static Student addStudent(String courseId, String enrollmentCode)
7885
return student;
7986
}
8087
}
81-
// [END classroom_add_student]
88+
// [END classroom_add_student]

Diff for: classroom/snippets/src/main/java/AddTeacher.java

+25-22
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,58 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
1615
// [START classroom_add_teacher]
1716

17+
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
1818
import com.google.api.client.googleapis.json.GoogleJsonError;
1919
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
20-
import com.google.api.client.http.HttpRequestInitializer;
2120
import com.google.api.client.http.javanet.NetHttpTransport;
2221
import com.google.api.client.json.gson.GsonFactory;
2322
import com.google.api.services.classroom.Classroom;
2423
import com.google.api.services.classroom.ClassroomScopes;
2524
import com.google.api.services.classroom.model.Teacher;
26-
import com.google.auth.http.HttpCredentialsAdapter;
27-
import com.google.auth.oauth2.GoogleCredentials;
2825
import java.io.IOException;
29-
import java.util.Collections;
26+
import java.security.GeneralSecurityException;
27+
import java.util.ArrayList;
28+
import java.util.Arrays;
3029

3130
/* Class to demonstrate the use of Classroom Add Teacher API */
3231
public class AddTeacher {
32+
33+
/* Scopes required by this API call. If modifying these scopes, delete your previously saved
34+
tokens/ folder. */
35+
static ArrayList<String> SCOPES =
36+
new ArrayList<>(Arrays.asList(ClassroomScopes.CLASSROOM_ROSTERS));
37+
3338
/**
3439
* Add teacher to a specific course.
3540
*
36-
* @param courseId - Id of the course.
41+
* @param courseId - Id of the course.
3742
* @param teacherEmail - Email address of the teacher.
3843
* @return newly created teacher
3944
* @throws IOException - if credentials file not found.
45+
* @throws GeneralSecurityException - if a new instance of NetHttpTransport was not created.
4046
*/
4147
public static Teacher addTeacher(String courseId, String teacherEmail)
42-
throws IOException {
43-
/* Load pre-authorized user credentials from the environment.
44-
TODO(developer) - See https://developers.google.com/identity for
45-
guides on implementing OAuth2 for your application. */
46-
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
47-
.createScoped(Collections.singleton(ClassroomScopes.CLASSROOM_ROSTERS));
48-
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
49-
credentials);
48+
throws GeneralSecurityException, IOException {
5049

51-
// Create the classroom API client
52-
Classroom service = new Classroom.Builder(new NetHttpTransport(),
53-
GsonFactory.getDefaultInstance(),
54-
requestInitializer)
55-
.setApplicationName("Classroom samples")
56-
.build();
50+
// Create the classroom API client.
51+
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
52+
Classroom service =
53+
new Classroom.Builder(
54+
HTTP_TRANSPORT,
55+
GsonFactory.getDefaultInstance(),
56+
ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES))
57+
.setApplicationName("Classroom samples")
58+
.build();
5759

5860
Teacher teacher = new Teacher().setUserId(teacherEmail);
5961
try {
6062
// Add a teacher to a specified course
6163
teacher = service.courses().teachers().create(courseId, teacher).execute();
6264
// Prints the course id with the teacher name
63-
System.out.printf("User '%s' was added as a teacher to the course with ID '%s'.\n",
65+
System.out.printf(
66+
"User '%s' was added as a teacher to the course with ID '%s'.\n",
6467
teacher.getProfile().getName().getFullName(), courseId);
6568
} catch (GoogleJsonResponseException e) {
6669
// TODO(developer) - handle error appropriately
@@ -76,4 +79,4 @@ public static Teacher addTeacher(String courseId, String teacherEmail)
7679
return teacher;
7780
}
7881
}
79-
// [END classroom_add_teacher]
82+
// [END classroom_add_teacher]

0 commit comments

Comments
 (0)