You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Django+Python环境中创建Google Meet链接时遇HttpError报错

解决Google Calendar API创建Meet会议时的"Invalid conference type value"错误

核心问题分析

你遇到的400错误主要源于两个关键点:

  1. 服务账号未通过域范围委派模拟目标用户身份,导致无法访问用户的primary日历并创建Meet会议
  2. 会议类型参数或API字段使用不符合当前API规范

修复方案

1. 配置服务账号的域范围委派并模拟用户身份

服务账号本身没有自己的primary日历,必须模拟目标教师的邮箱身份才能操作其个人日历:

  • 登录Google Admin控制台,找到你的服务账号,启用域范围委派
  • 添加https://www.googleapis.com/auth/calendar到授权的API范围列表中
  • 修改凭证初始化代码,加入用户模拟逻辑:
# 加载服务账号凭证
credentials = GoogleCredentials.from_service_account_file(
    service_account_file_path,
    scopes=['https://www.googleapis.com/auth/calendar']
)
# 模拟教师的邮箱账号(需确保该邮箱属于你的Google Workspace域)
delegated_credentials = credentials.with_subject(teacher.email)
# 使用委派后的凭证构建服务实例
service = build('calendar', 'v3', credentials=delegated_credentials)

2. 修正会议类型参数(可选)

虽然官方文档标注Google Meet对应的type为hangoutsMeet,但部分场景下需替换为meet:

'conferenceSolutionKey': {
    'type': 'meet'  # 替换原有的'hangoutsMeet'
},

3. 更新会议链接获取逻辑

旧字段hangoutLink已逐步被废弃,改用API推荐的conferenceData结构获取链接:

meet_link = created_event.get('conferenceData', {}).get('entryPoints', [{}])[0].get('uri')

完整修复后的代码

import logging
import uuid

from google.oauth2.service_account import Credentials as GoogleCredentials
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

from courses.models import Teacher


logger = logging.getLogger('google_calendar')

def create_google_meet_event(teacher: Teacher, start_time, end_time, summary, description):
    service_account_file_path = teacher.service_account_file.path

    # 初始化带域范围委派的凭证
    credentials = GoogleCredentials.from_service_account_file(
        service_account_file_path,
        scopes=['https://www.googleapis.com/auth/calendar']
    )
    delegated_credentials = credentials.with_subject(teacher.email)
    service = build('calendar', 'v3', credentials=delegated_credentials)

    event = {
        'summary': summary,
        'description': description,
        'start': {
            'dateTime': start_time.isoformat(),
            'timeZone': 'UTC',
        },
        'end': {
            'dateTime': end_time.isoformat(),
            'timeZone': 'UTC',
        },
        'conferenceData': {
            'createRequest': {
                'requestId': uuid.uuid4().hex,
                'conferenceSolutionKey': {
                    'type': 'meet'
                },
            }
        }
    }

    try:
        created_event = service.events().insert(
            calendarId='primary', 
            conferenceDataVersion=1, 
            body=event
        ).execute()
        # 获取最新格式的Meet链接
        meet_link = created_event.get('conferenceData', {}).get('entryPoints', [{}])[0].get('uri')
        return meet_link

    except HttpError as error:
        logger.error(f"Google Calendar API请求失败: {error}")
        raise

验证步骤

  1. 确认教师邮箱属于你的Google Workspace域
  2. 确认服务账号已被授予域范围委派权限,且API范围配置正确
  3. 测试创建事件,检查返回的Meet链接是否有效

内容的提问来源于stack exchange,提问作者lmoore

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 17:44:53