Da bi pristup API-ju bio dozvoljen potrebno se prijaviti (login) u sustav koristeći Authentication service API (URI: https://www.evisitor.hr/eVisit...spNetFormsAuth/Authentication/), koji implementira slijedeće metode:
Login
Interface: (string UserName, string Password, bool PersistCookie) -> bool
Primjer request data: {"UserName":"myusername","Password":"mypassword","PersistCookie":false}
Odgovor je true pri uspješnom loginu, inače false. Pri uspješnom loginu odgovor servera sadrži i niz cookie-a (authentication, affinity, language) koji se moraju slati prilikom svakog poziva API REST servisa.
Shvaćam da vraća JSON i da moram napraviti POST request, pa sam napravio sljedeće:
Code:
def http = new HTTPBuilder( 'https://www.evisitor.hr/testAp...FormsAuth/Authentication/Login' )
http.request(POST) {
uri.path = 'https://www.evisitor.hr/testAp...FormsAuth/Authentication/Login'
requestContentType = ContentType.JSON
body = [UserName:Constants.cornaro_username, Password: Constants.cornaro_password, PersistCookie: false]
response.success = { resp ->
println(response)
println "Success! ${resp.status}"
}
response.failure = { resp ->
println(response.status)
println "Request failed with status ${resp.status}"
}
}
def http = new HTTPBuilder( 'https://www.evisitor.hr/testAp...FormsAuth/Authentication/Login' )
http.request(POST) {
uri.path = 'https://www.evisitor.hr/testAp...FormsAuth/Authentication/Login'
requestContentType = ContentType.JSON
body = [UserName:Constants.cornaro_username, Password: Constants.cornaro_password, PersistCookie: false]
response.success = { resp ->
println(response)
println "Success! ${resp.status}"
}
response.failure = { resp ->
println(response.status)
println "Request failed with status ${resp.status}"
}
}
ali uopće ne dobijem nikakav true kako je navedeno u opisu dokumentacije niti niz cookija, već dobijem sljedeći response:
Code:
[failure:com.manas.frontdesk.EvisitorController$_testevisitor_closure1_closure3@51f4087a, success:com.manas.frontdesk.EvisitorController$_testevisitor_closure1_closure2@22372362]
Success! 200
[failure:com.manas.frontdesk.EvisitorController$_testevisitor_closure1_closure3@51f4087a, success:com.manas.frontdesk.EvisitorController$_testevisitor_closure1_closure2@22372362]
Success! 200
pa mi nije jasno kako response daje kod 200, a ne dobijam dobar povratni JSON? Ukoliko će biti od pomoći, našao sam ekvivalent onog što meni treba napisan u C# i u PHP-u:
Code:
/* Login section */
echo "<u>Login</u><br>";
$username = "username";
$password = "password";
$data = array("UserName" => $username, "Password" => $password, "PersistCookie" => "false");
$data_string = json_encode($data);
$resource = "Login";
$login_url = $authenticationServiceUrl.$resource;
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$response = curl_exec($ch);
$responseCode = curl_getinfo($ch)['http_code'];
$cookieContent = get_headers_from_curl_response($response)["Set-Cookie"];
echo "Cookie content: ".$cookieContent."<br>";
/* Login section */
echo "<u>Login</u><br>";
$username = "username";
$password = "password";
$data = array("UserName" => $username, "Password" => $password, "PersistCookie" => "false");
$data_string = json_encode($data);
$resource = "Login";
$login_url = $authenticationServiceUrl.$resource;
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$response = curl_exec($ch);
$responseCode = curl_getinfo($ch)['http_code'];
$cookieContent = get_headers_from_curl_response($response)["Set-Cookie"];
echo "Cookie content: ".$cookieContent."<br>";
Code:
public static IEnumerable<RestResponseCookie> Login()
{
var restRequest = new RestRequest()
{
Method = Method.POST,
Resource = "Login",
RequestFormat = DataFormat.Json,
};
restRequest.AddBody(new { UserName = Settings.Default.Username, Password = Settings.Default.Password, PersistCookie = false });
var restClient = new RestClient(Settings.Default.AuthenticationServiceUrl);
var response = restClient.Execute(restRequest);
return response.Content == "true" ? response.Cookies : null;
}
public static IEnumerable<RestResponseCookie> Login()
{
var restRequest = new RestRequest()
{
Method = Method.POST,
Resource = "Login",
RequestFormat = DataFormat.Json,
};
restRequest.AddBody(new { UserName = Settings.Default.Username, Password = Settings.Default.Password, PersistCookie = false });
var restClient = new RestClient(Settings.Default.AuthenticationServiceUrl);
var response = restClient.Execute(restRequest);
return response.Content == "true" ? response.Cookies : null;
}
Svaka pomoć je dobrodošla