【问题标题】:GSuite - Google Classroom API - List all courses in a domainGSuite - Google Classroom API - 列出域中的所有课程
【发布时间】:2020-02-19 19:26:21
【问题描述】:

我正在尝试使用 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')))

【问题讨论】:

    标签: python api google-classroom


    【解决方案1】:

    正如Manage Courses 的文档Retrieve course details 所述:

    注意:学生和教师只能检索自己的课程。管理员可以检索所有课程。

    因此,您需要使用具有管理员权限的用户才能检索您域中的所有课程。

    【讨论】:

      猜你喜欢
      • 2021-02-21
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-24
      • 2022-11-10
      • 1970-01-01
      相关资源
      最近更新 更多