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

如何用token通过Heroku Ruby gem认证?CLI正常但Ruby代码报401

Heroku流水线查询:CLI正常但Ruby代码报401未授权

问题现象

我能通过Heroku CLI成功列出流水线(已隐藏真实名称):

$ heroku pipelines
=== My Pipelines
...-qa-pipeline
...-qa-pipeline
...-qa-pipeline

但使用Ruby的platform-api库实现相同操作时,收到401 Unauthorized错误:

require "platform-api"
heroku = PlatformAPI.connect_token(`heroku auth:token`.strip)
heroku.pipeline.list

具体报错信息:

/home/pupeno/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/excon-0.99.0/lib/excon/middlewares/expects.rb:13:in `response_call': Expected([200, 201, 202, 204, 206, 304, 429]) <=> Actual(401 Unauthorized) (Excon::Error::Unauthorized)
        from /home/pupeno/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/excon-0.99.0/lib/excon/middlewares/response_parser.rb:12:in `response_call'                                                                                     
        from /home/pupeno/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/excon-0.99.0/lib/excon/connection.rb:459:in `response'                                                                                                          
        from /home/pupeno/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/excon-0.99.0/lib/excon/connection.rb:290:in `request'                                                                                                           
        from /home/pupeno/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/heroics-0.1.2/lib/heroics/link.rb:118:in `block in request_with_cache'                                                                                          
        from /home/pupeno/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/rate_throttle_client-0.1.2/lib/rate_throttle_client/clients/exponential_increase_proportional_remaining_decrease.rb:17:in `call'                                
        from /home/pupeno/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/heroics-0.1.2/lib/heroics/link.rb:117:in `request_with_cache'                                                                                                   
        from /home/pupeno/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/heroics-0.1.2/lib/heroics/link.rb:68:in `run'                                                                                                                  
        from /home/pupeno/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/heroics-0.1.2/lib/heroics/resource.rb:28:in `method_missing'                                                                                                   
        from /home/pupeno/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/platform-api-3.5.0/lib/platform-api/client.rb:2610:in `list'                                                                                                   
        from (irb):3:in `<main>'

请问我遗漏了什么?


排查与解决建议

  • 验证Token有效性:手动执行heroku auth:token复制输出内容,用curl直接测试API:
    curl -H "Authorization: Bearer YOUR_TOKEN" https://api.heroku.com/pipelines
    
    如果curl也返回401,说明Token过期或权限不足,执行heroku login重新登录刷新Token。
  • 检查Token获取结果:在irb中先执行puts heroku auth:token.strip,确认输出的是完整、无多余空格/换行的有效Token字符串。
  • 确认账号权限:如果流水线属于团队资源,需确保当前Token对应的账号拥有该团队流水线的读取权限。
  • 升级依赖库版本:当前使用的platform-api为3.5.0,尝试升级到最新版本,解决可能存在的API兼容性问题:
    gem update platform-api
    
  • 显式指定API端点:修改连接代码,明确指定Heroku API地址,避免环境配置问题:
    heroku = PlatformAPI.connect_token(`heroku auth:token`.strip, url: "https://api.heroku.com")
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 16:40:16