我正在为我的一个Rails应用程序设置Doorkeeper和OAuth2。我的目标是允许用户根据他们的access_token访问api,这样只有用户才能看到他们的'user_show‘json。到目前为止,我已经在“OAuth2/ applications”路线上设置和授权了我的开发和生产应用程序。
我的'/config/initializers/doorkeeper.rb‘
Doorkeeper.configure do
# Change the ORM that doorkeeper will use.
# Currently supported options are :active_record, :mongoid2, :mongoid3,
# :mongoid4, :mongo_mapper
orm :active_record
# This block will be called to check whether the resource owner is authenticated or not.
resource_owner_authenticator do
# Put your resource owner authentication logic here.
# Example implementation:
User.find_by_id(session[:current_user_id]) || redirect_to('/')
end
end我的‘/api/v1/user/controller.rb er.rb’看起来是这样的:
class Api::V1::UserController < Api::ApiController
include ActionController::MimeResponds
before_action :doorkeeper_authorize!
def index
user = User.find(doorkeeper_token.resource_owner_id)
respond_with User.all
end
def show
user = User.find(doorkeeper_token.resource_owner_id)
respond_with user
end
end我曾尝试访问OAuth应用程序表,以查看正在创建的内容,但无法在rails控制台中访问它。
提前感谢你的洞察!
发布于 2015-02-11 21:32:55
看起来Doorkeeper没有找到任何令牌。
请确保您正在发送它,无论是使用?access_token=#{token}还是?bearer_token=#{token}的url,或者使用无记名授权在报头中提供此令牌。
您还需要记住,令牌只能与应用程序相关联,而不需要资源所有者。因此即使使用有效令牌,resource_owner_id值也可以是nil。这取决于您使用的授权流(客户端凭据流与资源所有者没有关联)。请参阅https://github.com/doorkeeper-gem/doorkeeper/wiki#flows
对于OAuth表,可以尝试在rails控制台中使用Doorkeeper::AccessToken.all。
希望这能有所帮助
https://stackoverflow.com/questions/28097174
复制相似问题