Skip to content

Commit 8718e29

Browse files
feat: added Guardian and Guardian Invitation code snippets. (googleworkspace#528)
* 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
1 parent e49fb41 commit 8718e29

17 files changed

+868
-98
lines changed

classroom/snippets/build.gradle

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ repositories {
77

88
dependencies {
99
implementation 'com.google.api-client:google-api-client:2.0.0'
10-
implementation 'com.google.auth:google-auth-library-oauth2-http:1.11.0'
10+
implementation 'com.google.oauth-client:google-oauth-client-jetty:1.34.1'
1111
implementation 'com.google.apis:google-api-services-classroom:v1-rev20220323-2.0.0'
1212
testImplementation 'junit:junit:4.13.2'
13+
14+
/** This will be removed once all the classes have been updated to use the
15+
* ClassroomCredentials class. */
16+
implementation 'com.google.auth:google-auth-library-oauth2-http:1.11.0'
1317
}
1418

1519
test {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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_cancel_guardian_invitation_class]
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 com.google.api.services.classroom.model.GuardianInvitation;
25+
import java.io.IOException;
26+
import java.util.Collections;
27+
import java.util.List;
28+
29+
/* Class to demonstrate the use of Classroom Patch Guardian Invitation API. */
30+
public class CancelGuardianInvitation {
31+
/**
32+
* Cancel a guardian invitation by modifying the state of the invite.
33+
*
34+
* @param studentId - the id of the student.
35+
* @param invitationId - the id of the guardian invitation to modify.
36+
* @return - the modified guardian invitation.
37+
* @throws IOException - if credentials file not found.
38+
*/
39+
public static GuardianInvitation cancelGuardianInvitation(String studentId, String invitationId)
40+
throws Exception {
41+
42+
/* Scopes required by this API call. If modifying these scopes, delete your previously saved
43+
tokens/ folder. */
44+
final List<String> SCOPES =
45+
Collections.singletonList(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS);
46+
47+
// Create the classroom API client
48+
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
49+
Classroom service =
50+
new Classroom.Builder(
51+
HTTP_TRANSPORT,
52+
GsonFactory.getDefaultInstance(),
53+
ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES))
54+
.setApplicationName("Classroom samples")
55+
.build();
56+
57+
// [START classroom_cancel_guardian_invitation_code_snippet]
58+
59+
GuardianInvitation guardianInvitation = null;
60+
61+
try {
62+
/* Change the state of the GuardianInvitation from PENDING to COMPLETE. See
63+
https://developers.google.com/classroom/reference/rest/v1/userProfiles.guardianInvitations#guardianinvitationstate
64+
for other possible states of guardian invitations. */
65+
GuardianInvitation content =
66+
service.userProfiles().guardianInvitations().get(studentId, invitationId).execute();
67+
content.setState("COMPLETE");
68+
69+
guardianInvitation =
70+
service
71+
.userProfiles()
72+
.guardianInvitations()
73+
.patch(studentId, invitationId, content)
74+
.set("updateMask", "state")
75+
.execute();
76+
77+
System.out.printf(
78+
"Invitation (%s) state set to %s\n.",
79+
guardianInvitation.getInvitationId(), guardianInvitation.getState());
80+
} catch (GoogleJsonResponseException e) {
81+
// TODO (developer) - handle error appropriately
82+
GoogleJsonError error = e.getDetails();
83+
if (error.getCode() == 404) {
84+
System.out.printf(
85+
"There is no record of studentId (%s) or invitationId (%s).", studentId, invitationId);
86+
} else {
87+
throw e;
88+
}
89+
} catch (Exception e) {
90+
throw e;
91+
}
92+
return guardianInvitation;
93+
94+
// [END classroom_cancel_guardian_invitation_code_snippet]
95+
}
96+
}
97+
// [END classroom_cancel_guardian_invitation_class]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
import com.google.api.client.auth.oauth2.Credential;
16+
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
17+
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
18+
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
19+
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
20+
import com.google.api.client.http.javanet.NetHttpTransport;
21+
import com.google.api.client.json.JsonFactory;
22+
import com.google.api.client.json.gson.GsonFactory;
23+
import com.google.api.client.util.store.FileDataStoreFactory;
24+
import java.io.FileNotFoundException;
25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.io.InputStreamReader;
28+
import java.util.Collection;
29+
30+
public class ClassroomCredentials {
31+
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
32+
private static final String TOKENS_DIRECTORY_PATH = "tokens";
33+
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";
34+
35+
/**
36+
* Creates an authorized Credential object.
37+
*
38+
* @param HTTP_TRANSPORT The network HTTP Transport.
39+
* @param SCOPES The scopes required to make the API call.
40+
* @return An authorized Credential object.
41+
* @throws IOException If the credentials.json file cannot be found.
42+
*/
43+
static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT, Collection SCOPES)
44+
throws IOException {
45+
// Load client secrets.
46+
InputStream in = ClassroomCredentials.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
47+
if (in == null) {
48+
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
49+
}
50+
51+
GoogleClientSecrets clientSecrets =
52+
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
53+
54+
// Build flow and trigger user authorization request.
55+
GoogleAuthorizationCodeFlow flow =
56+
new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
57+
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
58+
.setAccessType("offline")
59+
.build();
60+
61+
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
62+
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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_create_guardian_invitation_class]
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 com.google.api.services.classroom.model.GuardianInvitation;
25+
import java.io.IOException;
26+
import java.util.Collections;
27+
import java.util.List;
28+
29+
/* Class to demonstrate the use of Classroom Create Guardian Invitation API. */
30+
public class CreateGuardianInvitation {
31+
/**
32+
* Creates a guardian invitation by sending an email to the guardian for confirmation.
33+
*
34+
* @param studentId - the id of the student.
35+
* @param guardianEmail - email to send the guardian invitation to.
36+
* @return - the newly created guardian invitation.
37+
* @throws IOException - if credentials file not found.
38+
*/
39+
public static GuardianInvitation createGuardianInvitation(String studentId, String guardianEmail)
40+
throws Exception {
41+
42+
/* Scopes required by this API call. If modifying these scopes, delete your previously saved
43+
tokens/ folder. */
44+
final List<String> SCOPES =
45+
Collections.singletonList(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS);
46+
47+
// Create the classroom API client
48+
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
49+
Classroom service =
50+
new Classroom.Builder(
51+
HTTP_TRANSPORT,
52+
GsonFactory.getDefaultInstance(),
53+
ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES))
54+
.setApplicationName("Classroom samples")
55+
.build();
56+
57+
// [START classroom_create_guardian_invitation_code_snippet]
58+
59+
GuardianInvitation guardianInvitation = null;
60+
61+
/* Create a GuardianInvitation object with state set to PENDING. See
62+
https://developers.google.com/classroom/reference/rest/v1/userProfiles.guardianInvitations#guardianinvitationstate
63+
for other possible states of guardian invitations. */
64+
GuardianInvitation content =
65+
new GuardianInvitation()
66+
.setStudentId(studentId)
67+
.setInvitedEmailAddress(guardianEmail)
68+
.setState("PENDING");
69+
try {
70+
guardianInvitation =
71+
service.userProfiles().guardianInvitations().create(studentId, content).execute();
72+
73+
System.out.printf("Invitation created: %s\n", guardianInvitation.getInvitationId());
74+
} catch (GoogleJsonResponseException e) {
75+
// TODO (developer) - handle error appropriately
76+
GoogleJsonError error = e.getDetails();
77+
if (error.getCode() == 404) {
78+
System.out.printf("There is no record of studentId: %s", studentId);
79+
} else {
80+
throw e;
81+
}
82+
} catch (Exception e) {
83+
throw e;
84+
}
85+
return guardianInvitation;
86+
87+
// [END classroom_create_guardian_invitation_code_snippet]
88+
}
89+
}
90+
// [END classroom_create_guardian_invitation_class]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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_delete_guardian_class]
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.util.Collections;
26+
import java.util.List;
27+
28+
/* Class to demonstrate the use of Classroom Delete Guardian API. */
29+
public class DeleteGuardian {
30+
/**
31+
* Delete a guardian for a specific student.
32+
*
33+
* @param studentId - the id of the student the guardian belongs to.
34+
* @param guardianId - the id of the guardian to delete.
35+
* @throws IOException - if credentials file not found.
36+
*/
37+
public static void deleteGuardian(String studentId, String guardianId) throws Exception {
38+
/* Scopes required by this API call. If modifying these scopes, delete your previously saved
39+
tokens/ folder. */
40+
final List<String> SCOPES =
41+
Collections.singletonList(ClassroomScopes.CLASSROOM_GUARDIANLINKS_STUDENTS);
42+
43+
// Create the classroom API client
44+
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
45+
Classroom service =
46+
new Classroom.Builder(
47+
HTTP_TRANSPORT,
48+
GsonFactory.getDefaultInstance(),
49+
ClassroomCredentials.getCredentials(HTTP_TRANSPORT, SCOPES))
50+
.setApplicationName("Classroom samples")
51+
.build();
52+
53+
// [START classroom_delete_guardian_code_snippet]
54+
try {
55+
service.userProfiles().guardians().delete(studentId, guardianId).execute();
56+
System.out.printf("The guardian with id %s was deleted.\n", guardianId);
57+
} catch (GoogleJsonResponseException e) {
58+
GoogleJsonError error = e.getDetails();
59+
if (error.getCode() == 404) {
60+
System.out.printf("There is no record of guardianId (%s).", guardianId);
61+
}
62+
}
63+
// [END classroom_delete_guardian_code_snippet]
64+
}
65+
}
66+
// [END classroom_delete_guardian_class]

0 commit comments

Comments
 (0)