2012-02-08 22 views
6

Próbuję wysłać żądanie do usługi REST (HP FEV 11 REST API fwiw) za pomocą klienta odpoczynku i nadal uzyskiwanie nieautoryzowanej odpowiedzi. Możliwe, że nie podążam za dokumentami, ale także nie jestem pewien, czy poprawnie wykonuję nagłówki. Do tej pory moje szukanie w Google dla RestClient było bezowocne. Każda pomoc będzie mile widziane:Jak wykonać podstawowe uwierzytelnianie Railsów za pomocą programu RestClient?

Kod:

@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 

Na tej podstawie SO question Próbowałem również następujące bez powodzenia:

@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 

Dokumentacja:

klient wysyła ważny Podstawowy nagłówek uwierzytelniania do punktu uwierzytelnienia punkt.

GET/qcbin/authentication-punktowy/uwierzytelnienia Autoryzacja: Podstawowe abcde123

Server zatwierdza podstawowe nagłówki uwierzytelniania, tworzy nowy LW-SSO żeton i zwraca go jako LWSSO_COOKIE_KEY.

Odpowiedz

7

Dobrze ... więc najpierw to pomaga, jeśli pójdę do prawej URL:

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

Który powinien brzmieć:

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

drugie, to pomaga, jeśli czytam docs dla RestClient zamiast po prostu spojrzeć na plik readme. Przykład pod numerem Instance Method Details bardzo pomógł.

Mój kod wygląda teraz tak:

@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 ja naprawdę na to przemyślana. Mogłem wybrać:

response = RestClient.get "http://#{@user_name}:#{@user_password}@alm_url/qcbin/authentication-point/authenticate" 
Powiązane problemy