2012-02-08 12 views
6

Sto tentando di inviare una richiesta a un servizio REST (FWI API REST di HP ALM 11) utilizzando rest-client e continuando a ricevere la risposta non autorizzata. Potrebbe essere che non sto seguendo i documenti, ma anche io non sono sicuro di fare le intestazioni correttamente. Finora il mio googling per RestClient è stato infruttuoso. Qualsiasi aiuto sarebbe apprezzato:Come si esegue l'autorizzazione di base Rails con RestClient?

Codice:

@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 

Sulla base di questo SO question Ho anche provato il seguente senza successo:

@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 

Documentazione:

client invia una valida Intestazione di autenticazione di base per il punto di autenticazione .

GET/qcbin/Autenticazione-point/autenticazione Autorizzazione: Base abcde123

Server convalida le intestazioni di autenticazione di base, crea una nuova LW-SSO token e restituisce come LWSSO_COOKIE_KEY.

risposta

7

Va bene ... quindi prima aiuta se vado al URL destra:

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

Che dovrebbe leggere:

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

In secondo luogo, è utile se ho letto la documentazione per RestClient piuttosto che guardare il readme. L'esempio sotto Instance Method Details ha aiutato molto.

Il mio codice ora assomiglia:

@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 ho veramente over-pensato che questo. Potrei andare con:

response = RestClient.get "http://#{@user_name}:#{@user_password}@alm_url/qcbin/authentication-point/authenticate" 
Problemi correlati