您的用户是通过 Google Meet 使用 Google 课堂吗?请查看 Apps 脚本快速入门 - 了解如何在 Google Meet 课程中查看学生出席情况

Python 快速入门

使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。

快速入门介绍了如何设置和运行调用 Google Workspace API 的应用。

Google Workspace 快速入门使用 API 客户端库来处理身份验证和授权流程的一些详细信息。我们建议您为自己的应用使用客户端库。每个快速入门都要求您开启身份验证和授权,然后才能运行示例应用。如果您不熟悉 Google Workspace API 的身份验证和授权,请参阅身份验证和授权概览

创建一个向 Google 课堂 API 发出请求的 Python 命令行应用。

目标

  • 设置环境。
  • 安装客户端库。
  • 设置示例。
  • 运行示例。

前提条件

如需运行本快速入门,您需要具备以下前提条件:

  • 启用了 Google 课堂的 Google for Education 帐号。

设置您的环境

如需完成本快速入门,请设置您的环境。

启用 API

在使用 Google API 之前,您需要先在 Google Cloud 项目中启用它们。您可以在单个 Google Cloud 项目中启用一个或多个 API。
  • 在 Google Cloud 控制台中,启用 Google 课堂 API。

    启用 API

为桌面应用授权凭据

如需以最终用户的身份进行身份验证并访问应用中的用户数据,您需要创建一个或多个 OAuth 2.0 客户端 ID。客户端 ID 用于向 Google 的 OAuth 服务器标识单个应用。如果您的应用在多个平台上运行,您必须为每个平台创建单独的客户端 ID。
  1. 在 Google Cloud 控制台中,转到“菜单”图标 > API 和服务 > 凭据

    进入“凭据”页面

  2. 点击创建凭据 > OAuth 客户端 ID
  3. 依次点击应用类型 > 桌面应用
  4. 名称字段中,输入凭据名称。此名称仅显示在 Google Cloud 控制台中。
  5. 点击创建。系统会显示 OAuth 客户端创建的屏幕,其中会显示您的新客户端 ID 和客户端密钥。
  6. 点击 OK。新创建的凭据会显示在 OAuth 2.0 客户端 ID 下。
  7. 将下载的 JSON 文件另存为 credentials.json,并将该文件移至工作目录。

安装 Google 客户端库

  • 安装 Python 版 Google 客户端库:

    pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
    

配置示例

  1. 在工作目录中,创建一个名为 quickstart.py 的文件。
  2. quickstart.py 中添加以下代码:

    课堂/快速入门/快速入门
    from __future__ import print_function
    
    import os.path
    
    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import build
    from googleapiclient.errors import HttpError
    
    # If modifying these scopes, delete the file token.json.
    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.json 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.json'):
            creds = Credentials.from_authorized_user_file('token.json', SCOPES)
        # 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.json', 'w') as token:
                token.write(creds.to_json())
    
        try:
            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.')
                return
            # Prints the names of the first 10 courses.
            print('Courses:')
            for course in courses:
                print(course['name'])
    
        except HttpError as error:
            print('An error occurred: %s' % error)
    
    
    if __name__ == '__main__':
        main()

运行示例

  1. 在工作目录中,构建并运行示例:

    python3 quickstart.py
    
  2. 首次运行示例时,系统会提示您授予访问权限:

    1. 如果您尚未登录 Google 帐号,系统会提示您登录。如果您登录了多个帐号,请选择一个帐号用于授权。
    2. 点击接受

    授权信息存储在文件系统中,因此在您下次运行示例代码时,系统不会提示您授权。

您已成功创建了第一个向 Google Classroom API 发出请求的 Python 应用。

后续步骤