【问题标题】:Grape token auth "error": "404 API Version Not Found"Grape token auth "error": "404 API Version Not Found"
【发布时间】:2016-11-18 08:04:11
【问题描述】:

我正在使用 rails 应用程序,设置葡萄,并尝试验证所有端点。所以尝试了葡萄令牌身份验证https://github.com/mcordell/grape_token_auth 并按照 github 文档中的说明进行设置。这些是我的配置。

初始化程序/gta.rb

GrapeTokenAuth.setup! do |config|
  config.mappings = { user: User }
  config.secret   = 'aaa'
end

对于 api,这是文件夹结构

app/api/tmo/api.rb -> mounted two version root file
app/api/tmo/vl/root.rb -> mounted all the other files for v1
app/api/tmo/vl/restaurants.rb -> here lies the mount authentication

include GrapeTokenAuth::MountHelpers
include GrapeTokenAuth::TokenAuthentication
mount_registration(to: '/auth', for: :user)
mount_sessions(to: '/auth', for: :user)
mount_token_validation(to: '/auth', for: :user)
mount_confirmation(to: '/auth', for: :user)

所以我的路线是这样的

Running via Spring preloader in process 9309
POST       /auth/api/v1(/.:format)
DELETE     /auth/api/v1(/.:format)
PUT        /auth/api/v1(/.:format)
POST       /auth/api/v1/sign_in(.:format)
DELETE     /auth/api/v1/sign_out(.:format)
GET        /auth/api/v1/validate_token(.:format)
GET        /auth/confirmation/api/v1(/.:format)
GET        /api/v1/statuses/public_timeline(.:format)

当我访问 /api/v1/statuses/public_timeline 时,它​​说 401 未授权,所以 authenticate_user!方法有效

但是当我发布到 /auth/api/v1 时,我收到以下错误

{  
  "error": "404 API Version Not Found"
}

如何检查错误发生的位置或如何解决?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 grape-api


    【解决方案1】:

    我在尝试设置 grape_token_auth 时遇到了完全相同的问题。我通过执行以下操作解决了它:

    namespace :auth do
      mount_registration(to: '/', for: :user)
      mount_sessions(to: '/', for: :user)
      mount_token_validation(to: '/', for: :user)
      mount_confirmation(to: '/ ', for: :user)
    end
    

    在这种情况下,您可以调用v1/auth/sign_out,并且应该可以工作。如果没有命名空间,您将直接以这种方式调用端点:v1/sign_out

    不确定是有意还是 Grape API 发生了变化,但使用 to: '/auth' 的默认配置会将身份验证端点的根更改为 /auth,这意味着您将不得不调用像 @987654328 这样的端点@并通过其他方式将version 提供给Grape,这样它就不会抱怨了。

    原帖于https://github.com/mcordell/grape_token_auth/issues/52#issuecomment-277449284

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-26
      相关资源
      最近更新 更多