Ruby on rails 如何使用带有Grape的\u http\u令牌进行身份验证\u或\u请求\u?
如何在grapeapi中使用\uhttp\u令牌实现authenticate\u或\urequest\u。下面是我的代码:Ruby on rails 如何使用带有Grape的\u http\u令牌进行身份验证\u或\u请求\u?,ruby-on-rails,ruby,curl,grape,grape-api,Ruby On Rails,Ruby,Curl,Grape,Grape Api,如何在grapeapi中使用\uhttp\u令牌实现authenticate\u或\urequest\u。下面是我的代码: module Articles class ArticleData < Grape::API include ActionController::HttpAuthentication::Token::ControllerMethods # http_basic do |email, password| # u
module Articles
class ArticleData < Grape::API
include ActionController::HttpAuthentication::Token::ControllerMethods
# http_basic do |email, password|
# user = User.find_by_email(email)
# user && user.valid_password?(password)
# end
before do
error!("401 Unauthorized", 401) unless authenticated
end
helpers do
def authenticated
authenticate_or_request_with_http_token do |token, options|
apiKey = ApiKey.where(auth_token: token).first
#ApiKey.exists?(access_token: token)
end
end
end
resource :article_data do
desc "Return all article data"
get do
Article.all
end
desc "create a new article"
## This takes care of parameter validation
params do
requires :title, type: String
requires :author, type: String
#requires :content, type: Text
end
#This takes care of creating article
post do
Article.create!({
title:params[:title],
content:params[:content],
author:params[:author],
user_id:params[:user_id],
image_url:params[:image_url]
})
end
desc "update article"
# this takes care of parameter validation
params do
requires :image_url, type: String
end
put ':id' do
Article.find(params[:id]).update({
content:params[:content],
image_url:params[:image_url]
})
end
desc "delete article by id"
# this takes care of parameter validation
params do
requires :id, type: String
end
delete ':id' do
Article.find(params[:id]).destroy!
end
end
end
end
模块文章
类ArticleData
当我运行curl命令:curl时,我得到了这个错误NoMethodError(未定义的方法'authenticate_或'u request_with#u http_token'):。任何帮助都将不胜感激。提前感谢。显示您的应用程序控制器。