门卫耙任务中未初始化的常量门卫:: AccessToken

时间:2019-03-04 08:13:23

标签: ruby-on-rails rake-task grape-api doorkeeper

我已经在Rails项目(Grape API)中使用Doorkeeper Gem实现了OAuth 2。我想通过rake任务删除已过期已撤销 access_token。到目前为止,我一直在关注官方的门卫文档并偶然发现rake task。据此,我在Doorkeeper::Rake.load_tasks中添加了Rakefile。之后,当我在控制台中运行rake doorkeeper:db:cleanup时,出现以下错误。

rake aborted!
NameError: uninitialized constant Doorkeeper::AccessToken
/.rvm/gems/ruby-2.6.0p0/ruby/2.6.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:74:in `block in load_missing_constant'
/.rvm/gems/ruby-2.6.0p0/ruby/2.6.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:8:in `without_bootsnap_cache'
/.rvm/gems/ruby-2.6.0p0/ruby/2.6.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:74:in `rescue in load_missing_constant'
/.rvm/gems/ruby-2.6.0p0/ruby/2.6.0/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:56:in `load_missing_constant'
/.rvm/gems/ruby-2.6.0p0/ruby/2.6.0/gems/doorkeeper-5.0.2/lib/doorkeeper/rake/db.rake:16:in `block (4 levels) in <main>'

我的 Rakefile.rb

require_relative 'config/application'
Doorkeeper::Rake.load_tasks
Rails.application.load_tasks

我也读过Cleanup AccessGrant and AccessTokenAdd a rake task to cleanup stale database records,但这都不能解决我的问题。我该如何解决?

更新 /。rvm / gems / ruby​​-2.6.0p0 / ruby​​ / 2.6.0 / gems / doorkeeper-5.0.2 / lib / doorkeeper.rb 文件

require 'doorkeeper/version'
require 'doorkeeper/engine'
require 'doorkeeper/config'

require 'doorkeeper/request/strategy'
require 'doorkeeper/request/authorization_code'
require 'doorkeeper/request/client_credentials'
require 'doorkeeper/request/code'
require 'doorkeeper/request/password'
require 'doorkeeper/request/refresh_token'
require 'doorkeeper/request/token'

require 'doorkeeper/errors'
require 'doorkeeper/server'
require 'doorkeeper/request'
require 'doorkeeper/validations'

require 'doorkeeper/oauth/authorization/code'
require 'doorkeeper/oauth/authorization/context'
require 'doorkeeper/oauth/authorization/token'
require 'doorkeeper/oauth/authorization/uri_builder'
require 'doorkeeper/oauth/helpers/scope_checker'
require 'doorkeeper/oauth/helpers/uri_checker'
require 'doorkeeper/oauth/helpers/unique_token'

require 'doorkeeper/oauth'
require 'doorkeeper/oauth/scopes'
require 'doorkeeper/oauth/error'
require 'doorkeeper/oauth/base_response'
require 'doorkeeper/oauth/code_response'
require 'doorkeeper/oauth/token_response'
require 'doorkeeper/oauth/error_response'
require 'doorkeeper/oauth/pre_authorization'
require 'doorkeeper/oauth/base_request'
require 'doorkeeper/oauth/authorization_code_request'
require 'doorkeeper/oauth/refresh_token_request'
require 'doorkeeper/oauth/password_access_token_request'

require 'doorkeeper/oauth/client_credentials/validation'
require 'doorkeeper/oauth/client_credentials/creator'
require 'doorkeeper/oauth/client_credentials/issuer'
require 'doorkeeper/oauth/client_credentials/validation'
require 'doorkeeper/oauth/client/credentials'

require 'doorkeeper/oauth/client_credentials_request'
require 'doorkeeper/oauth/code_request'
require 'doorkeeper/oauth/token_request'
require 'doorkeeper/oauth/client'
require 'doorkeeper/oauth/token'
require 'doorkeeper/oauth/token_introspection'
require 'doorkeeper/oauth/invalid_token_response'
require 'doorkeeper/oauth/forbidden_token_response'

require 'doorkeeper/models/concerns/orderable'
require 'doorkeeper/models/concerns/scopes'
require 'doorkeeper/models/concerns/expirable'
require 'doorkeeper/models/concerns/revocable'
require 'doorkeeper/models/concerns/accessible'

require 'doorkeeper/models/access_grant_mixin'
require 'doorkeeper/models/access_token_mixin'
require 'doorkeeper/models/application_mixin'

require 'doorkeeper/helpers/controller'

require 'doorkeeper/rails/routes'
require 'doorkeeper/rails/helpers'

require 'doorkeeper/rake'
require 'doorkeeper/stale_records_cleaner'

require 'doorkeeper/orm/active_record'

module Doorkeeper
  def self.authenticate(request, methods = Doorkeeper.configuration.access_token_methods)
    OAuth::Token.authenticate(request, *methods)
  end
end

1 个答案:

答案 0 :(得分:1)

您是否尝试先为rake任务加载应用程序环境?

Doorkeeper模型使用带有ActiveSupport钩子(on_load(:active_record)的延迟加载,因此,如果不加载应用程序env或AR,则可以跳过模型初始化,这就是Rake任务会错过必需类的原因。