GSuite - Google Classroom API - Список всех курсов в домене - PullRequest
0 голосов
/ 19 февраля 2020

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

что мне делать?

Вот код:

def connect():
    """Shows basic usage of the Classroom API.
    Prints the names of the first 10 courses the user has access to.
    """
    global service
    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)  

def listCourses()->None:
    global service  

    courses = []
    page_token = None

    while True:
        response = service.courses().list(pageToken=page_token,
                                          pageSize=100).execute()
        courses.extend(response.get('courses', []))
        page_token = response.get('nextPageToken', None)
        if not page_token:
            break

    if not courses:
        print('No courses found.')
    else:
        print('Courses:')
        for course in courses:
            print('{0} ({1})'.format(course.get('name'), course.get('id')))

1 Ответ

0 голосов
/ 20 февраля 2020

Как документы на Управление курсами состояния в Получение сведений о курсе :

Примечание : студенты и преподаватели могут только получить свои собственные курсы. Администраторы могут получить все курсы.

Поэтому вам нужно будет использовать пользователя с правами администратора, чтобы иметь возможность получить все курсы в вашем домене.

...