Manage Topics

A Topic resource represents a group of stream items categorized by similarity, such as the week assigned or course subject.

Each topic is identified by a unique ID assigned by the server. Associated with this ID is the course ID the topic belongs to, the actual topic name displayed on the Classroom UI, and the date and time of the last update.

Create a course

You can create a new topic in a course using the topics.create() method, as shown in the following sample:

Python

topic = {
    "name": 'Example Topic'
}
response = service.courses().topics().create(
  courseId=<course ID or alias>,
  body=topic).execute()
print('Topic created: ', response['name'])

Retrieve topic details

You can retrieve topics of a corresponding course with the topics.get() method, as shown in the following sample:

Python

response = service.courses().topics().get(
  courseId=<course ID or alias>,
  id=<topic ID>).execute()
print('{0} ({1})'.format(response['name'], response['topicId']))

For a list of courses, use the topics.list() method, as shown in the following sample:

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 topics

You can update an existing topic's name with the topics.patch() method, as shown in the following sample:

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']))