如何使用RestClient进行Rails基本授权?

Sco*_*hea 6 ruby-on-rails rest-client ruby-on-rails-3.1

我正在尝试使用rest-client向REST服务(HP ALM 11 REST API fwiw)发布请求并继续获取未经授权的响应.可能是我没有正确地遵循文档,但我也不确定我正在正确地处理标题.到目前为止,我在Google上搜索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
Run Code Online (Sandbox Code Playgroud)

基于这个SO问题,我也尝试了以下但没有成功:

@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
Run Code Online (Sandbox Code Playgroud)

文档:

客户端将有效的基本身份验证标头发送到身份验证点.

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

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

Sco*_*hea 7

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

authentication_url = @alm_url + "rest/is-authenticate"
Run Code Online (Sandbox Code Playgroud)

哪个应该是:

authentication_url = @alm_url + "authentication-point/authenticate"
Run Code Online (Sandbox Code Playgroud)

其次,如果我阅读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
Run Code Online (Sandbox Code Playgroud)

编辑:

哇,我真的过度思考了这一点.我本可以去的:

response = RestClient.get "http://#{@user_name}:#{@user_password}@alm_url/qcbin/authentication-point/authenticate"
Run Code Online (Sandbox Code Playgroud)