如何使用从RoR应用程序到另一个RoR应用程序的HTTP POST请求来处理AuthenticityToken值?

我正在使用Ruby on Rails 3,我想知道如何使用从RoR应用程序到另一个RoR应用程序的HTTP POST请求来处理AuthenticityToken值。在这种情况下,我的目的是提交一个登录表格,如果他提供了正确的
email
password
值,则以JSON格式返回用户信息。 我在这个URL上有一个RoR应用程序
pjtnam.com
和此URL上的另一个RoR应用程序
users.pjtname.com
如果我从应用程序
pjtname.com
向应用程序
users.pjtname.com
发出HTTP POST请求(在本例中我使用了Typhoeus gem)
response = Typhoeus::Request.post("http://users.pjtname.com/authentications",
             :params => {
               :new_authentication => {
                 :email    => email,
                 :password => password
               }
             }
           )
我收到了这个回复
<h1>
  ActionController::InvalidAuthenticityToken
    in AuthenticationsController#create
</h1>
<pre>ActionController::InvalidAuthenticityToken</pre>
那么,如何在安全方法模式中处理AuthenticityToken值?我想知道应用程序何时位于同一服务器上以及何时不存在。 在
http://users.pjtname.com/authentications/new
我有以下用于登录用户的表格:
<%= form_for(:new_authentication) do |f| %>
  <%= f.label :email %>
  <%= f.label :password %>

  <%= f.submit "Sign in" %>
<% end %>
在authentications_controller.rb我有
def create
  # Note ':authentication' symbol is different than ':new_authentication' seen in HTTP POST parameters and in the above form
  @authentication = Authentication.new(params[:authentication])

  @account = Account.sign_in_account(params[:new_authentication][:email], params[:new_authentication][:password])

  ...

  respond_to do |format|
    format.html {
      redirect_to @account
    }
    format.js {
      render(:update) { |page|
        page.redirect_to @account
      }
    }
    format.json {
      render :json => @account
    }
  end
end
在routes.rb我有
  resources :authentications do #, :path => "authentication" do
    member do
      get  'confirm_authentication'
      post 'confirm_authentication'
    end
  end
更新@idlefingers的答案 请求
Typhoeus::Request.post("http://users.pjtname.com/authentications/new",
# or
# Typhoeus::Request.post("http://users.pjtname.com/authentications",
   :headers => {"Content-Type" => "application/json"},
   :params => { ... } # Same parameters as above
   }
 )
响应
<h1>
  StandardError
</h1>
<pre>Invalid JSON string</pre>
请求
Typhoeus::Request.post("http://users.pjtname.com/authentications/new.json",
   :params => { ... } # Same parameters as above
   }
 )
响应
<h1>Routing Error</h1>
<p><pre>No route matches &quot;/authentications/new.json&quot;</pre></p>
    
已邀请:
看起来它没有使用正确的内容类型发送请求。如果内容类型是application / xml或application / json,Rails应该跳过真实性令牌检查,以便它可以与API一起使用,而不必完全禁用真实性令牌。 我不知道Typhoeus gem,但看起来你可能只需要将“.json”或“.xml”添加到url(取决于你实现的API),或者可能需要将其传递给标题哈希中的选项。     

要回复问题请先登录注册