【问题标题】:How do I do Rails Basic Authorization with RestClient?如何使用 RestClient 进行 Rails 基本授权?
【发布时间】:2012-02-08 20:16:45
【问题描述】:

我正在尝试使用 rest-client 向 REST 服务 (HP ALM 11 REST API fwiw) 发布请求并不断收到未经授权的响应。可能是我没有正确遵循文档,但我也不确定我是否正确地处理了标题。到目前为止,我对 RestClient 的谷歌搜索一直没有结果。任何帮助将不胜感激:

代码:

@alm_url       = "http://alm_url/qcbin/"
@user_name     = "username"
@user_password = "password"

authentication_url = @alm_url + "rest/is-authenticate"
resource = RestClient::Resource.new authentication_url
resource.head :Authorization => Base64.encode64(@user_name) + ":" + Base64.encode64(@user_password)
response = resource.get


#response = RestClient.get authentication_url, :authorization => @username, @user_password
Rails.logger.debug response.inspect

基于这个SO question我还尝试了以下但没有成功:

@alm_url       = "http://alm_url/qcbin/"
@user_name     = "username"
@user_password = "password"

authentication_url = @alm_url + "rest/is-authenticate"
resource = RestClient::Resource.new authentication_url, {:user => @user_name, :password => @user_password}
response = resource.get


#response = RestClient.get authentication_url, :authorization => @username, @user_password
Rails.logger.debug response.inspect

文档:

客户端向身份验证发送一个有效的基本身份验证标头 点。

GET /qcbin/authentication-point/authenticate 授权:基本 ABCDE123

服务器验证基本身份验证标头,创建一个新的 LW-SSO 令牌并将其作为 LWSSO_COOKIE_KEY 返回。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.1 rest-client


    【解决方案1】:

    好的...首先,如果我转到正确的 URL 会有所帮助:

    authentication_url = @alm_url + "rest/is-authenticate"
    

    应改为:

    authentication_url = @alm_url + "authentication-point/authenticate"
    

    其次,如果我阅读 RestClient 的文档而不是只看自述文件,这会有所帮助。 Instance Method Details 下的例子帮助很大。

    我的代码现在看起来像:

    @alm_url       = "http://alm_url/qcbin/"
    @user_name     = "username"
    @user_password = "password"
    
    authentication_url = @alm_url + "authentication-point/authenticate"
    resource = RestClient::Resource.new(authentication_url, @user_name, @user_password)
    response = resource.get
    
    Rails.logger.debug response.inspect
    

    编辑:

    哇,我真的想多了。我本来可以去的:

    response = RestClient.get "http://#{@user_name}:#{@user_password}@alm_url/qcbin/authentication-point/authenticate"
    

    【讨论】:

      猜你喜欢
      • 2013-04-13
      • 2021-09-08
      • 2016-05-09
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      相关资源
      最近更新 更多