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

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

我有以下代码(在 python 中,用于 google 教室 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()

它 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

我也希望它 return 与课程作业一起使用。

到目前为止,它只是获取课程名称和 ID。 我正在尝试制作一个可以检索和使用课程作业的应用程序。

此外,是否有任何我没有使用的 ID?比如,课程作业 = results.get('[Other ID]', [])

任何帮助将不胜感激,谢谢!

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()