gpt4 book ai didi

ruby-on-rails - 如何使用 RestClient 进行 Rails 基本授权?

转载 作者:行者123 更新时间:2023-12-04 14:49:28 26 4
gpt4 key购买 nike

我正在尝试使用 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

文档:

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.

最佳答案

好的...所以首先,如果我访问正确的 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"

关于ruby-on-rails - 如何使用 RestClient 进行 Rails 基本授权?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9200725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com