Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails HTTP令牌:访问被拒绝_Ruby On Rails_Mongoid_Access Token - Fatal编程技术网

Ruby on rails HTTP令牌:访问被拒绝

Ruby on rails HTTP令牌:访问被拒绝,ruby-on-rails,mongoid,access-token,Ruby On Rails,Mongoid,Access Token,通过浏览器访问时,我收到消息“**HTTP令牌:访问被拒绝**”http://localhost:3000/api/v1/tasks.json?auth_token=szVkqLnUbdzbekV8B-n 但当我从终端访问时,它正在成功curlhttp://localhost:3000/api/v1/moments.json -H'授权:Token Token=“szVkqLnUbdzbekV8B-n\” 这里是代码 class Api::V1::TaskController < Ap

通过浏览器访问时,我收到消息“
**HTTP令牌:访问被拒绝**
”http://localhost:3000/api/v1/tasks.json?auth_token=szVkqLnUbdzbekV8B-n 但当我从终端访问时,它正在成功
curlhttp://localhost:3000/api/v1/moments.json -H'授权:Token Token=“szVkqLnUbdzbekV8B-n\”

这里是代码

  class Api::V1::TaskController < ApplicationController
          before_action :autentifikasi
    def index
        @tasks = current_user.tasks
    end
        private
          def autentifikasi
            authenticate_or_request_with_http_token('Premium') do |token, options|
               @current_user = User.find_by(authentication_token: token)

            end
          end 
        end
     end 
class Api::V1::TaskController

谁来帮帮我!!我的代码有什么问题?

这是因为
使用\u http\u令牌验证\u或\u请求\u
需要
请求头中的
授权:令牌

在浏览器中将标题作为查询参数传递时,在cURL命令中设置标题


因此,请求头中没有令牌,因此当通过浏览器访问时,您的方法无法找到令牌

您的代码没有问题-错误在于您的测试方法

cURL示例正确地发送一个
Authorization:Token
头,并同时发送该令牌

请求<代码>http://localhost:3000/api/v1/tasks.json?auth_token=szVkqLnUbdzbekV8B-浏览器中的n
只需设置
参数['auth_token']
,因为它是一个查询参数。这当然会导致身份验证失败

Rails和大多数sane框架并不将HTTP头和查询参数视为等价物。这会让你的应用看起来像瑞士奶酪

如果你想通过浏览器测试基于令牌的身份验证,你应该使用一个插件,比如Postman,它允许你设置请求头。更好的方法是编写一个实际的自动化集成测试