更新YouTube视频权限时遇Request had insufficient authentication scopes错误
YouTube视频状态更新403权限错误修复
问题概述
尝试将YouTube视频从私有改为公开时触发403权限不足错误:
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://youtube.googleapis.com/youtube/v3/videos?part=id%2Csnippet%2Cstatus&alt=json returned "Request had insufficient authentication scopes.". Details: "[{'message': 'Insufficient Permission', 'domain': 'global', 'reason': 'insufficientPermissions'}]">
仅更新操作失败,上传视频的insert功能正常
核心错误及修复方案
1. 误用了API方法
你当前调用的service.videos().insert()是创建新视频的接口,要修改已有视频的状态,必须使用videos().update()方法。
修改YT_handler.py中的请求代码:
# 替换原有的insert调用 response_upload = service.videos().update( part='status', # 只需要更新status部分,无需id和snippet body=request_body, ).execute()
2. 权限范围不足+旧凭证缓存
当前使用的https://www.googleapis.com/auth/youtube.force-ssl权限仅支持上传视频,无法修改已有视频的隐私状态。需要升级权限并清除旧缓存:
操作步骤:
- 修改YT_handler.py的权限范围:
# 替换原SCOPES SCOPES = ['https://www.googleapis.com/auth/youtube']
- 删除旧的pickle文件(
token_youtube_v3.pickle),旧文件缓存了低权限的授权信息,必须重新生成新凭证才能生效。
修改后的完整YT_handler.py关键代码
update_public_request_body = { "id": "", 'status': { 'privacyStatus': 'public' } } API_NAME = 'youtube' API_VERSION = 'v3' # 更新为全权限范围 SCOPES = ['https://www.googleapis.com/auth/youtube'] def set_vid_public(CLIENT_SECRET_FILE: str, video_id: str, pickle_file: str = "") -> dict: print("[yt_handler] [set_vid_public] Authenicatin with Google account...") service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES, force_pickle_file=pickle_file) request_body = update_public_request_body request_body["id"] = video_id print("[yt_handler] [set_vid_public] Started updating video status...") # 使用正确的update方法 response_upload = service.videos().update( part='status', body=request_body, ).execute() print("[yt_handler] [set_vid_public] SUCCESS - Video status set to Public!") return response_upload
内容的提问来源于stack exchange,提问作者Shadi Shakkour
相关产品推荐
相关产品推荐

