Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Java evisitor REST API login

[es] :: Java :: Java evisitor REST API login

[ Pregleda: 1620 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

mensur.durakovic
Mensur Durakovic
Programer
MANAS
Split, Hrvatska

Član broj: 333759
Poruke: 2
195.29.107.*



+1 Profil

icon Java evisitor REST API login07.06.2016. u 10:06 - pre 95 meseci
Pozdrav, radim u Grails frameworku (Java, Groovy) i pokušavam se logirati na e-visitor API. Dokumentacija je jako loše i neprecizno opisana. Dakle, na linku https://www.evisitor.hr/eVisit...a_login_na_e-Visitor_Web_API_0 piše sljedeće :

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}"
            }
        }


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


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>";


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;
        }


Svaka pomoć je dobrodošla
 
Odgovor na temu

dejanet
Beograd

Član broj: 19240
Poruke: 1181



+836 Profil

icon Re: Java evisitor REST API login07.06.2016. u 11:10 - pre 95 meseci
Pitanje je sta oni salju kao response.

Na osnovu response koji si postovao:

[failure:com.manas.frontdesk.EvisitorController$_testevisitor_closure1_closure3@51f4087a, success:com.manas.frontdesk.EvisitorController$_testevisitor_closure1_closure2@22372362]
Success! 200

mislim da on nije serijalizovan u string.

Zaboravio sam grails, ali probaj ovako:

response.success = {resp, json ->.....

ili

response.success = { resp, reader ->
result = reader.getText()
}
 
Odgovor na temu

mensur.durakovic
Mensur Durakovic
Programer
MANAS
Split, Hrvatska

Član broj: 333759
Poruke: 2
195.29.107.*



+1 Profil

icon Re: Java evisitor REST API login07.06.2016. u 11:57 - pre 95 meseci
Uspio sam riješiti problem, malo sam promijenio kod. Cookie se nalazi u headeru, i daje mi dobar response:

Code:

        try {
            def ret = null
            def http = new HTTPBuilder("https://www.evisitor.hr/testAp...FormsAuth/Authentication/Login")

            // perform a POST request, expecting TEXT response
            http.request(POST, ContentType.JSON) {
                uri.path = "https://www.evisitor.hr/testAp...FormsAuth/Authentication/Login"
                body = [UserName:Constants.cornaro_username, Password: Constants.cornaro_password, PersistCookie: false]
                headers.'User-Agent' = 'Mozilla/5.0 Ubuntu/8.10 Firefox/3.0.4'

                // response handler for a success response code
                response.success = { resp, reader ->
                    println "response status: ${resp.statusLine}"
                    println 'Headers: -----------'
                    resp.headers.each { h ->
                        println " ${h.name} : ${h.value}"
                    }

                    ret = reader

                    println 'Response data: -----'
                    println ret
                    println '--------------------'
                }
            }
            return ret
        } catch (groovyx.net.http.HttpResponseException ex) {
            ex.printStackTrace()
            return null
        } catch (java.net.ConnectException ex) {
            ex.printStackTrace()
            return null
        }


i dobijem response:
Code:

response status: HTTP/1.1 200 OK
Headers: -----------
 Cache-Control : private
 Content-Length : 4
 Content-Type : application/json; charset=utf-8
 Server : Microsoft-IIS/8.5
 Set-Cookie : ASP.NET_SessionId=crvpq2chsy5ytsmomanaqdcj; path=/; HttpOnly
 Set-Cookie : .testAPI=E838680ECCF1824C8DD2ECF4AB2354757674E0AC817621673743A1480564551F720AE1987B23C3DB4A794467AF0C3387E22312DBE794D847E101E97D91D0B7B356664BCF46A56A963D593431D2A678434132E04DFAD4D423032FFE6A7E6B24042D0C8D00ED759B98F0404CE9AD27B34620A8573A677AB51402ABB44B352D920AD5DE62D04122F4165B1285005AAF85EB; path=/; HttpOnly
 X-AspNet-Version : 4.0.30319
 Set-Cookie : ARRAffinity=587094056ec65f6fb7887bbfa824b02ab38c399e7cead56e31a7345524cfcc70;Path=/;Domain=www.evisitor.hr
 X-Powered-By : ARR/2.5
 Date : Tue, 07 Jun 2016 09:24:09 GMT
Response data: -----
true
--------------------
 
Odgovor na temu

[es] :: Java :: Java evisitor REST API login

[ Pregleda: 1620 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.