Python GSuite-谷歌课堂API-列出域中的所有课程

Python GSuite-谷歌课堂API-列出域中的所有课程,python,api,google-classroom,Python,Api,Google Classroom,我试图使用GoogleAPI 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

我试图使用GoogleAPI 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')))
正如中的文档所述:

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

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