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

发布于 2025-01-03 23:06:18 字数 1440 浏览 5 评论 0原文

我正在尝试使用 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问题我也尝试了以下方法没有成功:

@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 返回。

I am trying to post a request to a REST service (HP ALM 11 REST API fwiw) using rest-client and keep getting the Unauthorized response. Could be I am not following the docs right but also I am not sure I am doing the headers properly. So far my googling for RestClient has been fruitless. Any help would be appreciated:

Code:

@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

Based on this SO question I also tried the following without success:

@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

Documentation:

Client sends a valid Basic Authentication header to the authentication
point.

GET /qcbin/authentication-point/authenticate Authorization: Basic
ABCDE123

Server validates the Basic authentication headers, creates a new
LW-SSO token and returns it as LWSSO_COOKIE_KEY.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離殇 2025-01-10 23:06:18

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

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

应该是:

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

其次,如果我阅读 RestClient 的文档而不是仅仅查看自述文件,它会有所帮助。 实例方法详细信息下的示例有很大帮助。

我的代码现在看起来像:

@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"

Okay... so first it helps if I go to the right URL:

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

Which should read:

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

Secondly, it helps if I read the docs for RestClient rather than just look at the readme. The example under Instance Method Details helped a lot.

My code now looks like:

@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

EDIT:

Wow I really over-thought this. I could have gone with:

response = RestClient.get "http://#{@user_name}:#{@user_password}@alm_url/qcbin/authentication-point/authenticate"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文