/ / RestClientでRailsの基本認証を行うにはどうすればよいですか? - ruby​​-on-rails、ruby-on-rails-3.1、rest-client

RestClientでRails Basic Authorizationを実行するにはどうすればよいですか? - ruby​​-on-rails、ruby-on-rails-3.1、rest-client

RESTサービスにリクエストを投稿しようとしています(HP ALM 11 REST API fwiw)rest-clientを使用し、Unauthorizedレスポンスを受け取り続けます。私はドキュメントの権利を守っていないのかもしれませんが、ヘッダを正しく処理しているのかどうかもわかりません。これまでのところ、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

これに基づいて だから質問 私も成功せずに次のことを試してみました:

@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として返します。

回答:

回答№1の場合は7

さて...それで最初に私は正しいURLに行けば役立ちます:

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

どちらを読む必要があります:

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

第二に、単にreadmeを見るのではなく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"