Search code examples
pythongoogle-classroom

Python Google Classroom API "TypeError: list indices must be integers or slices, not str"


I have the following code (In python, for the google classroom API)

from __future__ import print_function
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials

# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/classroom.courses.readonly','https://www.googleapis.com/auth/classroom.student-submissions.me.readonly']

def main():
    """Shows basic usage of the Classroom API.
    Prints the names of the first 10 courses the user has access to.
    """
    creds = None
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    service = build('classroom', 'v1', credentials=creds)

    # Call the Classroom API
    results = service.courses().list(pageSize=10).execute()
    courses = results.get('courses', [])
    coursework = results.get('courseWork', [])

    if not courses:
        print('No courses found.')
    else:
        print('Courses:')
        for course in courses:
            print(course['name'])
            print(course['id'])
            print(coursework['courses'])
            

if __name__ == '__main__':
    main()

And it returns

C:\Users\name\Code Projects\Classroom API - Python>python quickstart.py
Courses:
Fun Facts
(My Course ID)
Traceback (most recent call last):
  File "C:\Users\name\Code Projects\Classroom API - Python\quickstart.py", line 51, in <module>
    main()
  File "C:\Users\name\Code Projects\Classroom API - Python\quickstart.py", line 47, in main
    print(coursework['courses'])
TypeError: list indices must be integers or slices, not str

I want it to return with the Course Work, too.

So far, it just fetches the course name and ID. I am trying to make an app that can retrieve and use coursework.

Also, are there any IDs I'm not using? Like, coursework = results.get('[Other ID]', [])

Any help would be appreciated, thanks!


Solution

  • from __future__ import print_function
    import os.path
    from googleapiclient.discovery import build
    from google_auth_oauthlib.flow import InstalledAppFlow
    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    
    # If modifying these scopes, delete the file token.json.
    SCOPES = ['https://www.googleapis.com/auth/classroom.courses.readonly','https://www.googleapis.com/auth/classroom.student-submissions.me.readonly']
    
    def main():
        """Shows basic usage of the Classroom API.
        Prints the names of the first 10 courses the user has access to.
        """
        creds = None
        # The file token.json stores the user's access and refresh tokens, and is
        # created automatically when the authorization flow completes for the first
        # time.
        if os.path.exists('token.json'):
            creds = Credentials.from_authorized_user_file('token.json', SCOPES)
        # If there are no (valid) credentials available, let the user log in.
        if not creds or not creds.valid:
            if creds and creds.expired and creds.refresh_token:
                creds.refresh(Request())
            else:
                flow = InstalledAppFlow.from_client_secrets_file(
                    'credentials.json', SCOPES)
                creds = flow.run_local_server(port=0)
            # Save the credentials for the next run
            with open('token.json', 'w') as token:
                token.write(creds.to_json())
    
        service = build('classroom', 'v1', credentials=creds)
    
        # Call the Classroom API
        results = service.courses().list(pageSize=10).execute()
        courses = results.get('courses', [])
        # this line was the root of the error
        # it was caused due to the fact that results doesn't contain a key `courseWork`
        # so in your code it returns list [], thats why the error occured changing to a dict {} will fix it
        coursework = results.get('courseWork', {})
    
        if not courses:
            print('No courses found.')
        else:
            print('Courses:')
            for course in courses:
                print(course['name'])
                print(course['id'])
                if (work : = coursework.get('courseWork'):
                    print(work)
                else:
                    print("course not found")
                
    
    if __name__ == '__main__':
        main()