如何通过API向Google Classroom提交作业并更新学生作业完成状态?
Hey there! Let’s walk through exactly how to handle both syncing assignment completion status to Google Classroom and submitting assignments via the API—since you’ve already got the basics down, this should fit right into your existing integration.
When a student marks an assignment as complete in your app, you need to update their corresponding submission record in Google Classroom to reflect that. Here’s what you need to do:
- Use the
courses.courseWork.studentSubmissions.patchAPI: This lets you update specific fields of a student’s submission without overwriting the entire record. - Required IDs: You’ll need three key values: the
courseId(your target Classroom ID),courseWorkId(the ID of the assignment in question), anduserId(the student’s Google account ID—usemeif you’re acting on behalf of the currently authenticated student). - Update the state: In your request body, set the
statefield toTURNED_INto mark the assignment as submitted. If you’re handling teacher feedback later, you can also useRETURNEDwhen sending graded work back to the student. - Permissions: Make sure your app has the
classroom.coursework.studentsOAuth scope (orclassroom.coursework.meif you’re only acting on behalf of the authenticated user).
Example request structure (simplified):
PATCH https://classroom.googleapis.com/v1/courses/{courseId}/courseWork/{courseWorkId}/studentSubmissions/{userId} Content-Type: application/json { "state": "TURNED_IN" }
If you need to update statuses for multiple students at once, use courses.courseWork.studentSubmissions.batchUpdate instead—it’s much more efficient than making individual patch requests.
There are two main scenarios here, each with a dedicated API:
Scenario 1: Submit an assignment on behalf of a student
If you want to directly mark a student’s assignment as submitted (similar to them clicking "Turn in" in Classroom), use the courses.courseWork.studentSubmissions.turnIn API. This is a straightforward action that sets the submission state to TURNED_IN in one step.
- Required parameters: Same as above—
courseId,courseWorkId, anduserId. - Permissions: Use
classroom.coursework.mefor student-authenticated requests, orclassroom.coursework.studentsif you’re acting as an admin/teacher.
Example request:
POST https://classroom.googleapis.com/v1/courses/{courseId}/courseWork/{courseWorkId}/studentSubmissions/{userId}:turnIn Content-Type: application/json {}
Scenario 2: Create a new submission with attachments
If you need to upload files or links as part of the submission (like a student uploading a document from your app), use the courses.courseWork.studentSubmissions.create API first to create the submission record, then call turnIn to mark it as complete.
In the request body, specify the assignmentSubmission field with an attachments array—you can include Drive files, links, or other supported attachment types.
Example request (creating a submission with a Drive file):
POST https://classroom.googleapis.com/v1/courses/{courseId}/courseWork/{courseWorkId}/studentSubmissions Content-Type: application/json { "userId": "student@example.com", "assignmentSubmission": { "attachments": [ { "driveFile": { "id": "123abcXYZ" } } ] } }
- Always verify the current submission state with
courses.courseWork.studentSubmissions.getbefore making updates—this prevents accidental overwrites or duplicate submissions. - Make sure your OAuth token has the correct scopes; missing permissions are the most common cause of API errors here.
- Classroom sometimes caches submission data, so if you don’t see updates immediately, give it a minute to sync.
内容的提问来源于stack exchange,提问作者Innocent Criminal

