[Python] 403 Forbidden error when using Google Classroom Studentsubmission.patch

Discussão em 'Python' iniciado por Stack, Março 3, 2025.

  1. Stack

    Stack Membro Participativo

    def grade_submissions(service, course_id, coursework_id, submissions, grade):
    for submission in submissions:
    submission_id = submission.get('id') # Get the submission ID
    try:
    # Update the submission with the assigned grade
    response = service.courses().courseWork().studentSubmissions().patch(
    courseId=course_id,
    courseWorkId=coursework_id,
    id=submission_id,
    updateMask='assignedGrade',
    body={'assignedGrade': grade}
    ).execute()
    print(f"Graded submission ID: {submission_id} with grade: {grade}")
    except HttpError as error:
    print(f"An error occurred while grading submission ID {submission_id}: {error}")





    I'm getting a permission denied error, but it's weird because I'm able to call the studentSubmissions().list endpoint just fine. Please help me figure out where I'm going wrong.

    Here is my cred set up. I've tried updating the token json, but nothing seems to work

    SCOPES = [
    "https://www.googleapis.com/auth/classroom.courses", # Access to Classroom courses
    "https://www.googleapis.com/auth/classroom.coursework.students", # Access to student coursework
    "https://www.googleapis.com/auth/classroom.coursework.me", # Access to user's coursework
    "https://www.googleapis.com/auth/drive.readonly" # Read-only access to Google Drive
    ]

    """Shows basic usage of the Classroom API.
    Prints the names of the first 10 courses the user has access to.
    """
    creds = None # Initialize credentials variable to None

    # Check if the token.json file exists to load existing credentials
    # 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("new_token.json"):
    creds = Credentials.from_authorized_user_file("new_token.json", SCOPES) # Load credentials from file

    # 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()) # Refresh the credentials if they are expired
    else:
    # Initiate the OAuth2 flow using client secrets file
    flow = InstalledAppFlow.from_client_secrets_file(
    "credentials.json", SCOPES
    )
    creds = flow.run_local_server(port=0) # Run local server for user authentication
    # Save the credentials for the next run
    with open("new_token.json", "w") as token:
    token.write(creds.to_json()) # Write the credentials to token.json

    try:
    # Build the Classroom API service object
    service = build("classroom", "v1", credentials=creds)

    Continue reading...

Compartilhe esta Página