Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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脚本设计身份验证_Ruby_Ruby On Rails 3_Authentication_Rest_Devise - Fatal编程技术网

通过ruby脚本设计身份验证

通过ruby脚本设计身份验证,ruby,ruby-on-rails-3,authentication,rest,devise,Ruby,Ruby On Rails 3,Authentication,Rest,Devise,我有一个小应用程序,我将让一些外部应用程序通过http和rest将数据放到这个服务中。我已经让它工作,但没有身份验证。在门户中,我使用Desive,我的问题是:如何从ruby脚本级别验证门户?要先向以下脚本添加什么以进行身份验证?我想用Desive保护这个控制器,然后我需要以下脚本的身份验证部分 require "net/http" require "json" @host = "localhost" @port = 3000 @post_ws = "/external/rd" @req_b

我有一个小应用程序,我将让一些外部应用程序通过http和rest将数据放到这个服务中。我已经让它工作,但没有身份验证。在门户中,我使用Desive,我的问题是:如何从ruby脚本级别验证门户?要先向以下脚本添加什么以进行身份验证?我想用Desive保护这个控制器,然后我需要以下脚本的身份验证部分

require "net/http"
require "json"

@host = "localhost"
@port = 3000
@post_ws = "/external/rd"

@req_body = {
"device" => {
  "name" => "device_json", 
  "operating_system_id" => "7", 
  "hash_string" => "jfsg3k4ovj0j02jv", 
  "user_id" => "1"
}
}.to_json

req = Net::HTTP::Post.new(@post_ws, initheader = {'Content-Type' =>'application/json'})
req.body = @req_body
response = Net::HTTP.new(@host, @port).start {|http| http.request(req) }
问候,, Mateusz

以下是解决方案:

我使用了Desive提供的可认证令牌

这是如何用json实现它的一个很好的答案。我遇到了一些麻烦并描述了它们。答案也是有的

下面是示例代码:

require "net/http"
require "json"

@host = "localhost"
@port = 3000
@post_sign = "/users/sign_in.json"
@post_ws = "/external/rd"

@req_sign = {
"user" => {
  "email" => "some@email.com", 
  "password" => "123456" 
}
}.to_json

sig = Net::HTTP::Post.new(@post_sign, initheader = {'Content-Type' => 'application/json'})
sig.body = @req_sign


http = Net::HTTP.new(@host, @port).start
resp1 = http.request(sig)
puts "Response: #{resp1.code} , Message: #{resp1.message} , Body: #{resp1.body}"

if resp1.code == "200" then 
  puts "logged in"
  json_resp = JSON.parse(resp1.body)
  @auth_token = json_resp['auth_token']

  @req_body = {
  "device" => {
    "name" => "device_json", 
    "operating_system_id" => "7", 
    "hash_string" => "jfsg3k4ovj0j02jv" 
  }, 
  "auth_token" => @auth_token 
  }.to_json
  req = Net::HTTP::Post.new(@post_ws, initheader = {'Content-Type' =>'application/json'})
  req.body = @req_body

  response = http.request(req)
  puts "Response: #{response.code} , Message: #{response.message} , Body: #{response.body}"
end
问候,, Mateusz

以下是解决方案:

我使用了Desive提供的可认证令牌

这是如何用json实现它的一个很好的答案。我遇到了一些麻烦并描述了它们。答案也是有的

下面是示例代码:

require "net/http"
require "json"

@host = "localhost"
@port = 3000
@post_sign = "/users/sign_in.json"
@post_ws = "/external/rd"

@req_sign = {
"user" => {
  "email" => "some@email.com", 
  "password" => "123456" 
}
}.to_json

sig = Net::HTTP::Post.new(@post_sign, initheader = {'Content-Type' => 'application/json'})
sig.body = @req_sign


http = Net::HTTP.new(@host, @port).start
resp1 = http.request(sig)
puts "Response: #{resp1.code} , Message: #{resp1.message} , Body: #{resp1.body}"

if resp1.code == "200" then 
  puts "logged in"
  json_resp = JSON.parse(resp1.body)
  @auth_token = json_resp['auth_token']

  @req_body = {
  "device" => {
    "name" => "device_json", 
    "operating_system_id" => "7", 
    "hash_string" => "jfsg3k4ovj0j02jv" 
  }, 
  "auth_token" => @auth_token 
  }.to_json
  req = Net::HTTP::Post.new(@post_ws, initheader = {'Content-Type' =>'application/json'})
  req.body = @req_body

  response = http.request(req)
  puts "Response: #{response.code} , Message: #{response.message} , Body: #{response.body}"
end
问候,,
Mateusz

我看到token_authenticatable可能有帮助,但目前不知道如何从脚本获取token,因此仍然没有解决方案:/我看到token_authenticatable可能有帮助,但目前不知道如何从脚本获取token,因此仍然没有解决方案:/