我正在尝试从班级作业中获取成绩,但如果我尝试调用
service.courses().courseWork().list(courseId=course['id']).execute()
它给了我
Warning: Scope has changed from "https://www.googleapis.com/auth/classroom.courses.readonly https://www.googleapis.com/auth/classroom.coursework.me.readonly https://www.googleapis.com/auth/classroom.rosters.readonly" to "https://www.googleapis.com/auth/classroom.student-submissions.me.readonly https://www.googleapis.com/auth/classroom.courses.readonly https://www.googleapis.com/auth/classroom.rosters.readonly".
如果我将范围更改为推荐的范围,我就明白了。
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://classroom.googleapis.com/v1/courses/[id]/courseWork?alt=json returned "The caller does not have permission". Details: "The caller does not have permission">
我的代码:
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
import warnings
# Set up the OAuth2 flow
flow = InstalledAppFlow.from_client_secrets_file(
'client_secret.json',
scopes=[
"https://www.googleapis.com/auth/classroom.coursework.me.readonly",
"https://www.googleapis.com/auth/classroom.rosters.readonly",
"https://www.googleapis.com/auth/classroom.courses.readonly"
])
flow.run_local_server(port=0)
# Get the OAuth2 credentials
creds = flow.credentials
# You can then use the credentials to authenticate requests to the Google API
service = build('classroom', 'v1', credentials=creds)
# Get a list of all the courses that you are a part of
courses = service.courses().list().execute()
# Loop through the courses and print the course names and IDs
for course in courses['courses']:
print(f"Course Name: {course['name']}")
print(f"Course ID: {course['id']}")
cw = service.courses().courseWork().list(courseId=course['id']).execute()
for assig in cw['courseWork']:
print(assig)