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



我有以下代码(在python中,对于谷歌课堂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()

它返回

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

我希望它也能和课程作业一起返回。

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

还有,有没有我不用的身份证?比如,coursework=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()

相关内容

最新更新