如何使用带有葡萄的authenticate_or_request_with_http_token?

时间:2015-10-30 06:35:36

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

如何在grape API中实现authenticate_or_request_with_http_token。以下是我的代码:

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

我收到此错误 NoMethodError(未定义的方法`authenticate_or_request_with_http_token&#39; for#):当我运行curl命令时:curl http://localhost:3000/api/v1/article_data.json。任何帮助都感激不尽。提前谢谢。

0 个答案:

没有答案