A Topic is a label for grouping CourseWork and
CourseWorkMaterial stream items within a course. A Topic is typically used
to categorize these items by similarity, such as the week the items are assigned
or their subject. Users can visually organize and filter stream items in the
Classwork view of the Classroom UI.
Each Topic is identified by a unique ID assigned by the server. The Topic
resource also contains the following:
name: The display name shown in the Classroom UIupdateTime: The time theTopicwas last updatedcourseId: The ID of the course that theTopicis associated with
Create a Topic
You can create a new Topic in a course using the topics.create() method,
as shown in the following sample:
Java
Python
topic = {
"name": 'Example Topic'
}
response = service.courses().topics().create(
courseId=<course ID or alias>,
body=topic).execute()
print('Topic created: ', response['name'])
The name field is always required and must be a non-empty string. All other
fields are optional.
Retrieve Topic details
You can retrieve a specific Topic by ID with the topics.get() method, as
shown in the following sample:
Java
Python
response = service.courses().topics().get(
courseId=<course ID or alias>,
id=<topic ID>).execute()
print('{0} ({1})'.format(response['name'], response['topicId']))
Use the topics.list() method to retrieve all Topics in a course, as shown
in the following sample:
Java
Python
topics = []
page_token = None
while True:
response = service.courses().topics().list(
pageToken=page_token,
pageSize=30,
courseId=<course ID or alias>).execute()
topics.extend(response.get('topic', []))
page_token = response.get('nextPageToken', None)
if not page_token:
break
if not topics:
print('No topics found.')
else:
print('Topics:')
for topic in topics:
print('{0} ({1})'.format(topic['name'], topic['topicId']))
Update a Topic
You can update an existing Topic name with the topics.patch() method, as
shown in the following sample:
Java
Python
topic = {
"name": "New Topic Name"
}
response = service.courses().topics().patch(
courseId=<course ID or alias>,
id=<topic ID>,
updateMask="name",
body=topic).execute()
print('{0} ({1})'.format(response['name'], response['topicId']))
The Topic id and updateTime fields are server-generated and can't be
updated with the API.
Delete a Topic
You can delete an existing Topic with the topics.delete() method, as shown
in the following sample: