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

使用python-youtube SDK获取access token时未返回refresh token的问题

解决python-youtube SDK认证时无法获取Refresh Token的问题
  • 问题原因:要拿到refresh token,必须在授权阶段明确请求offline_access权限范围,你当前调用get_authorize_url()时没指定这个scope,所以Youtube不会返回refresh token。

  • 修改后的完整认证代码:

    cli = Client(client_id="[my client id]", client_secret="[my client secret]")
    # 添加offline_access权限,要求获取离线访问权限
    auth_url = cli.get_authorize_url(scopes=["https://www.googleapis.com/auth/youtube.upload", "offline_access"])
    print(auth_url)
    
    # 复制登录跳转后的完整URL
    token_response = cli.generate_access_token(authorization_response="[link from redirect]")
    print(token_response)
    # 此时返回的AccessToken对象会包含refresh_token字段
    
    # 后续可通过refresh token维持客户端授权(避免重复登录)
    # cli = Client(access_token="[返回的access_token]", refresh_token="[返回的refresh_token]", client_id="[my client id]", client_secret="[my client secret]")
    
  • 关键注意点:

    • 如果之前已经用不带offline_access的链接授权过,必须先在Google账户的安全设置里移除该应用的授权,再重新走授权流程,否则还是拿不到refresh token。
    • 确认OAuth客户端配置的重定向URI和你实际使用的完全一致,不然会导致授权失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 03:15:55