Как получить идентификатор курса для Google Classroom API - PullRequest
0 голосов
/ 01 апреля 2020

Я пытаюсь использовать Google Classroom API , я прочитал их документацию , и идентификатор курса используется практически для всего, но они так и не объяснили, где найти идентификатор курса для курса.

Также создается впечатление, что при создании курса функция возвращает идентификатор курса, но мне интересно, возможно ли получить идентификатор курса для курсов, которые уже существуют.

1 Ответ

2 голосов
/ 01 апреля 2020

Как показано на странице быстрого запуска документации (https://developers.google.com/classroom/quickstart/python), вы можете запустить фрагмент кода, чтобы перечислить первые 10 курсов, к которым у пользователя есть доступ, с их учетными данными. Затем вы можете добавить оператор print(course['id']) во время итерации по курсам, чтобы распечатать идентификатор курсов, которые вы получили. Пример python показан ниже

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

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/classroom.courses.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.pickle 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.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # 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.pickle', 'wb') as token:
            pickle.dump(creds, token)

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

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

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

if __name__ == '__main__':
    main()
...