1+ // Copyright 2022 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+
16+ // [START classroom_modify_attachments_student_submissions_class]
17+
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 .HttpRequestInitializer ;
21+ import com .google .api .client .http .javanet .NetHttpTransport ;
22+ import com .google .api .client .json .gson .GsonFactory ;
23+ import com .google .api .services .classroom .Classroom ;
24+ import com .google .api .services .classroom .ClassroomScopes ;
25+ import com .google .api .services .classroom .model .Attachment ;
26+ import com .google .api .services .classroom .model .Link ;
27+ import com .google .api .services .classroom .model .ModifyAttachmentsRequest ;
28+ import com .google .api .services .classroom .model .StudentSubmission ;
29+ import com .google .auth .http .HttpCredentialsAdapter ;
30+ import com .google .auth .oauth2 .GoogleCredentials ;
31+ import java .io .IOException ;
32+ import java .util .Arrays ;
33+ import java .util .Collections ;
34+
35+ /* Class to demonstrate the use of Classroom ModifyAttachments StudentSubmissions API. */
36+ public class ModifyAttachmentsStudentSubmission {
37+ /**
38+ * Modify attachments on a student submission.
39+ *
40+ * @param courseId - identifier of the course.
41+ * @param courseWorkId - identifier of the course work.
42+ * @param id - identifier of the student submission.
43+ * @return - the modified student submission.
44+ * @throws IOException - if credentials file not found.
45+ */
46+ public static StudentSubmission modifyAttachments (String courseId , String courseWorkId , String id )
47+ throws IOException {
48+ /* Load pre-authorized user credentials from the environment.
49+ TODO(developer) - See https://developers.google.com/identity for
50+ guides on implementing OAuth2 for your application. */
51+ GoogleCredentials credentials = GoogleCredentials .getApplicationDefault ()
52+ .createScoped (Collections .singleton (ClassroomScopes .CLASSROOM_COURSEWORK_STUDENTS ));
53+ HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter (
54+ credentials );
55+
56+ // Create the classroom API client.
57+ Classroom service = new Classroom .Builder (new NetHttpTransport (),
58+ GsonFactory .getDefaultInstance (),
59+ requestInitializer )
60+ .setApplicationName ("Classroom samples" )
61+ .build ();
62+
63+ // [START classroom_modify_attachments_student_submissions_code_snippet]
64+
65+ StudentSubmission studentSubmission = null ;
66+ try {
67+ // Create ModifyAttachmentRequest object that includes a new attachment with a link.
68+ Link link = new Link ().setUrl ("https://en.wikipedia.org/wiki/Irrational_number" );
69+ Attachment attachment = new Attachment ().setLink (link );
70+ ModifyAttachmentsRequest modifyAttachmentsRequest = new ModifyAttachmentsRequest ()
71+ .setAddAttachments (Arrays .asList (attachment ));
72+
73+ // The modified studentSubmission object is returned with the new attachment added to it.
74+ studentSubmission = service .courses ().courseWork ().studentSubmissions ().modifyAttachments (
75+ courseId , courseWorkId , id , modifyAttachmentsRequest )
76+ .execute ();
77+
78+ /* Prints the modified student submission. */
79+ System .out .printf ("Modified student submission attachments: '%s'.\n " , studentSubmission
80+ .getAssignmentSubmission ()
81+ .getAttachments ());
82+ } catch (GoogleJsonResponseException e ) {
83+ //TODO (developer) - handle error appropriately
84+ GoogleJsonError error = e .getDetails ();
85+ if (error .getCode () == 404 ) {
86+ System .out .printf ("The courseId (%s), courseWorkId (%s), or studentSubmissionId (%s) does "
87+ + "not exist.\n " , courseId , courseWorkId , id );
88+ } else {
89+ throw e ;
90+ }
91+ } catch (Exception e ) {
92+ throw e ;
93+ }
94+ return studentSubmission ;
95+
96+ // [END classroom_modify_attachments_student_submissions_code_snippet]
97+
98+ }
99+ }
100+ // [END classroom_modify_attachments_student_submissions_class]
0 commit comments