如何使用 Python 和 Zoom API 创建会议?


Zoom 是一个视频会议平台,越来越受到远程会议和网络研讨会的欢迎。Zoom 提供了一个 API,允许开发者以编程方式与 Zoom 的功能进行交互,包括创建和管理会议。在这种情况下,Python 提供了一种简单而高效的方法来通过 Zoom 的 API 创建会议。

使用 Python,您可以自动化创建 Zoom 会议的过程,并将其与其他工作流程或应用程序集成。在本指南中,我们将探讨如何使用 requests 库和 Zoom API 的身份验证机制,在 Python 中使用 Zoom API 创建会议。

本指南介绍如何使用 Python 和 Zoom API 创建 Zoom 会议。要使用 Zoom API,您必须首先按照以下步骤创建它:

  • 访问 https://marketplace.zoom.us/,注册或登录您的 Zoom 帐户。

  • 点击“开发”选项卡,然后选择“创建应用”。

  • 同意 Zoom 的 API 许可和使用条款。

  • 选择“JWT”作为应用程序类型,因为它易于使用。

  • 输入您的应用名称,然后点击“创建”。

  • 填写必填详细信息,例如您的公司名称、开发者姓名和电子邮件地址。对于公司名称,您可以输入您的姓名,然后点击“继续”。

  • 转到“应用凭据”选项卡,复制您的 API 密钥和 API 密钥,并保存它们。

在继续编写代码之前,我们需要安装以下软件包:

  • JWT  JWT(JSON Web 令牌)是一种紧凑的、URL 安全的表示要在双方之间传输的声明的方法。

  • Requests  Python 中的 requests 包用于向 Web API 发出 HTTP 请求。

  • JSON  Python 中的json 包用于编码和解码 JSON 数据。

我们可以使用以下命令安装这些软件包。

pip3 install jwt requests json

使用 Zoom API 创建会议

现在让我们关注代码。请考虑以下代码。

示例

import jwt import requests import json from time import time # Replace with your own API key and secret API_KEY = 'Your API key' API_SECRET = 'Your API secret' # Create a function to generate a token using the PyJWT library def generate_token(): # Create a payload of the token containing API key and expiration time token_payload = {'iss': API_KEY, 'exp': time() + 5000} # Secret used to generate token signature secret_key = API_SECRET # Specify the hashing algorithm algorithm = 'HS256' # Encode the token token = jwt.encode(token_payload, secret_key, algorithm=algorithm) return token.decode('utf-8') # Create JSON data for the Zoom meeting details meeting_details = { "topic": "The title of your Zoom meeting", "type": 2, "start_time": "2019-06-14T10:21:57", "duration": "45", "timezone": "Europe/Madrid", "agenda": "test", "recurrence": { "type": 1, "repeat_interval": 1 }, "settings": { "host_video": "true", "participant_video": "true", "join_before_host": "False", "mute_upon_entry": "False", "watermark": "true", "audio": "voip", "auto_recording": "cloud" } } # Send a request with headers including a token and meeting details def create_zoom_meeting(): headers = { 'authorization': 'Bearer ' + generate_token(), 'content-type': 'application/json' } # Make a POST request to the Zoom API endpoint to create the meeting response = requests.post( f'https://api.zoom.us/v2/users/me/meetings', headers=headers, data=json.dumps(meeting_details) ) print("\nCreating Zoom meeting...\n") # Convert the response to JSON and extract the meeting details response_json = json.loads(response.text) join_url = response_json["join_url"] meeting_password = response_json["password"] # Print the meeting details print(f'\nHere is your Zoom meeting link {join_url} and your password: "{meeting_password}"\n') # Run the create_zoom_meeting function create_zoom_meeting()

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

解释

  • 代码导入必要的库  jwt、requests、jsontime

  • 代码定义了 API 密钥和密钥变量,稍后将在程序中使用。

  • 代码定义了一个名为 generateToken() 的函数,该函数使用 PyJWT 库创建用于身份验证的令牌。该函数编码包含 API 密钥和过期时间的有效负载,然后使用 HS256 哈希算法使用 API 密钥对有效负载进行签名。令牌作为 UTF-8 字符串返回。

  • 代码定义了一个名为 meetingdetails 的字典,其中包含 Zoom 会议的详细信息,例如标题、开始时间、持续时间和设置。

  • 代码定义了一个名为 createMeeting() 的函数,该函数向 Zoom API 端点发送 POST 请求以创建新会议。该函数首先调用 generateToken() 函数以获取身份验证令牌,然后设置请求的标头以包含令牌并将内容类型设置为 JSON。该函数以 JSON 编码字符串的形式在请求正文中发送 meetingdetails。如果请求成功,该函数将打印会议详细信息,例如加入 URL 和密码。

  • 代码调用 createMeeting() 函数来创建一个新的 Zoom 会议。

  • 代码使用注释来解释程序的每个部分的作用。

输出

运行此代码后,它将生成以下输出:

can you change the value in this
creating zoom meeting …
here is your zoom meeting link
https://us04web.zoom.us/j/12345678901?pwd=AbCdEfGhIjKlMnOpQrStUvWxYz and your password: "XyZaBc123"

结论

总而言之,使用 Python 中的 Zoom API 创建会议是一个简单的过程,可以使用 Python 的 Zoom API 包装器来实现。

按照本指南中概述的步骤,开发人员可以轻松地将其 Python 应用程序与 Zoom 会议集成,并为其用户自动化创建会议的过程。

更新于:2023年4月21日

3K+ 次浏览

开启您的职业生涯

通过完成课程获得认证

开始学习
广告