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

YouTube API中publishAt参数失效问题求助

解决YouTube API video.update设置publishAt返回400错误的问题

可能的原因及修复方案

1. 排程时间为过去时间

YouTube API要求publishAt必须设置为当前时间之后的UTC时间,你代码中写的'2022-12-29T02:00:00.000Z'是已过期的时间,这是触发错误的直接原因。

修复:用datetime生成未来的UTC时间,比如设置为当前时间2小时后:

from datetime import datetime, timedelta

future_time = datetime.utcnow() + timedelta(hours=2)
status["publishAt"] = future_time.isoformat() + "Z"

2. 复用原有status对象引发字段冲突

从playlistItems.list接口获取的status对象可能包含冗余字段(如license、embeddable),这些字段在update时可能触发不必要的校验逻辑。

修复:新建干净的status字典,只保留必要配置:

# 替换原有status修改逻辑
status = {
    "privacyStatus": "private",
    "selfDeclaredMadeForKids": False,
    "publishAt": future_time.isoformat() + "Z"
}

3. 排程时间设置过近

YouTube对排程时间有最小间隔限制(通常要求至少在当前时间15分钟之后),若设置的时间太近也会报错。

修复:确保排程时间距离当前时间至少30分钟以上。

4. 隐私状态配置错误

排程发布时,隐私状态需设为private或unlisted,若设为public会直接立即发布视频,忽略排程时间。你的代码中这部分配置是正确的,但需确保视频当前确实处于私有状态。

修改后的关键代码片段

from datetime import datetime, timedelta

# ... 其他代码保持不变 ...

# 获取目标私有视频
vid = privvids[-1]

# 处理snippet部分
snippet = vid["snippet"]
snippet["categoryId"] = 27  # 教育分类

# 生成未来排程时间并配置status
future_time = datetime.utcnow() + timedelta(hours=2)
status = {
    "privacyStatus": "private",
    "selfDeclaredMadeForKids": False,
    "publishAt": future_time.isoformat() + "Z"
}

# 执行更新请求
response = youtube.videos().update(
    part='snippet,status',
    body={
        "id": vid["contentDetails"]["videoId"],
        "snippet": snippet,
        "status": status
    }
).execute()

内容的提问来源于stack exchange,提问作者Алесандр Мерсер

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 18:21:33