GSuite - Google Classroom API - List all courses in a domain

筅森魡賤 提交于 2020-06-13 09:33:10

问题


I'm trying to list all courses in the domain using Google API python, but I can only list the courses associated with the user who makes the authentication.

what should I do?

Here is the code:

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:


As the Docs at Manage Courses states in Retrieve course details:

Note: Students and teachers can only retrieve their own courses. Administrators can retrieve all courses.

Therefore you will need to use a user with admin access in order to be able to retrieve all courses in your domain.



来源:https://stackoverflow.com/questions/60307803/gsuite-google-classroom-api-list-all-courses-in-a-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!