如何使用 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() 

解釋

  • 程式碼匯入必要的庫  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+ 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告