如何使用 RestClient 进行 Rails 基本授权?
我正在尝试使用 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
ABCDE123Server validates the Basic authentication headers, creates a new
LW-SSO token and returns it as LWSSO_COOKIE_KEY.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的...所以首先,如果我转到正确的 URL,它会有所帮助:
应该是:
其次,如果我阅读 RestClient 的文档而不是仅仅查看自述文件,它会有所帮助。 实例方法详细信息下的示例有很大帮助。
我的代码现在看起来像:
编辑:
哇我真的想太多了。我本可以选择:
Okay... so first it helps if I go to the right URL:
Which should read:
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:
EDIT:
Wow I really over-thought this. I could have gone with: