I'm trying to create a script that will assign students by ID to coursework. The code appears to be working, but I'm getting a 500 response from the server. Any idea what I'm doing wrong? I've tried different student ids and they all give the same result?
assign_coursework_to_students(service, course_id, coursework_id, student_ids_to_add):
"""Assign coursework to specific individual students with retry on internal error."""
print(student_ids_to_add)
for attempt in range(3): # Retry up to 3 times
try:
# Prepare the request body
body = {
"assigneeMode": "INDIVIDUAL_STUDENTS",
"modifyIndividualStudentsOptions": {
"addStudentIds": student_ids_to_add
}
}
# Make the API call to modify assignees
response = service.courses().courseWork().modifyAssignees(
courseId=course_id,
id=coursework_id,
body=body
).execute()
print(f"Successfully assigned coursework to students: {response['title']} (ID: {response['id']})")
return # Exit the function on success
except HttpError as error:
print(f"An error occurred: {error.resp.status} - {error._get_reason()}")
if error.resp.status == 500:
print(f"Internal error encountered. Retrying... (Attempt {attempt + 1})")
time.sleep(2) # Wait before retrying
continue # Retry the request
else:
print(f"An error occurred while assigning coursework: {error}")
return # Exit on non-500 error
except Exception as e:
print(f"An unexpected error occurred: {e}")
return # Exit on unexpected errors