Sakai Rest API login from Java

579 views
Skip to first unread message

Chaitanya Cheekate

unread,
Aug 17, 2015, 9:34:10 AM8/17/15
to saka...@apereo.org
Hi All,

I am logging into sakai using web service(/direct/session/new) but it's not saving the sessionId in cookie. Due to this I was unable to continue the session.

But it's working in below scenarios

Sakai Mobile App: In our mobile app, Session is persisting if I invoke the same login web service from Titanium appcelerator http client.

SakaiPy(Python API): This api using python's session.post which is able to set session cookie as per below code from the file.

session.post(self.baseURL+loginURL.format(connectionInfo['username'],connectionInfo['password']))
Can you please help me out.

Posted in stackoverflow also.
--
Thanks.

William Karavites

unread,
Aug 17, 2015, 10:11:05 AM8/17/15
to Chaitanya Cheekate, saka...@apereo.org
I'm the author of SakaiPy. I haven't had much time (recently) to do much work on it as the office has been busy. There's a TON more of what I want to accomplish with the library.

Once things settle down in the office I'll see how I can help you with storing the session using web service.

In the meantime, can you please give me/us examples of the code you're trying to use for storing the sessionid?

-Will

------------------------------------
William Karavites
Application Programmer
OIT/OIRT- Rutgers University
Office: 848-445-8726
Cell: 732-822-9405
will...@rutgers.edu
http://willkara.com
------------------------------------


From: "Chaitanya Cheekate" <chaitanya...@vidyayug.com>
To: saka...@apereo.org
Sent: Monday, August 17, 2015 9:33:46 AM
Subject: [sakai-dev] Sakai Rest API login from Java
--
You received this message because you are subscribed to the Google Groups "Sakai Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sakai-dev+...@apereo.org.
To post to this group, send email to saka...@apereo.org.
Visit this group at http://groups.google.com/a/apereo.org/group/sakai-dev/.



Chaitanya Cheekate

unread,
Aug 17, 2015, 10:17:13 AM8/17/15
to William Karavites, saka...@apereo.org
Hi William,

I forgot to mention that I am trying to make it work in java.

As of now I just wrote web service call to get the session id.


        URL url = new URL(webserviceURL);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
       
        conn.setRequestProperty("Content-Length", params.toString().length()
                + "");
        conn.connect();

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(params.toString());
        wr.flush();
        // Get the response
        InputStream is;
        System.out.println(conn.getResponseCode());
        if (conn.getResponseCode() == 200 || conn.getResponseCode()==201) {
            is = conn.getInputStream();
        } else {
            is = conn.getErrorStream();
        }
       
        String responseData = IOUtils.toString(is, "UTF-8");
        wr.close();
        System.out.println("responseData" + responseData);
 
  

Thank you.

--
Thanks.

Schauer, Christopher R

unread,
Aug 17, 2015, 3:49:58 PM8/17/15
to Chaitanya Cheekate, sakai-dev
The cookie is in the response, you'll either have to grab it from the 'Set-Cookie' response header yourself or use a library that manages cookies for you. The java.net API offers a CookieHandler class that will manage cookies system-wide. There's some documentation here: https://docs.oracle.com/javase/tutorial/networking/cookies/index.html.

You might also want to look into HttpClient (http://hc.apache.org/httpcomponents-client-ga/tutorial/html/) which is an alternative to HttpURLConnection. The cookie management is a bit easier to deal with on a per request basis in my opinion.

-Chris

Will Karavites

unread,
Aug 18, 2015, 9:11:33 AM8/18/15
to Chaitanya Cheekate, saka...@apereo.org
Chat,

We’re doing our major Summer deploy today (10-10.x). I’ll try to get back to you later today with some comments about the java code!

-Will

Chaitanya Cheekate

unread,
Aug 18, 2015, 10:18:37 AM8/18/15
to csch...@txstate.edu, saka...@apereo.org
Hi Chris,

Thanks for your suggestion. Using HttpClient API  I am able to hold the session.


Below is the Sample code:
//create httpclient
HttpClient httpClient = HttpClientBuilder.create().build();

//store cookies in context
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

//call login web service
HttpPost postRequest = new HttpPost(webserviceURL);
postRequest.setHeader("Content-Type", "application/x-www-form-urlencoded");
postRequest.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = httpClient.execute(postRequest, httpContext);

//get the status code
int status = response.getStatusLine().getStatusCode();

//if status code is 201 login is successful. So reuse the httpContext for next requests.
//if status code is not 201, there is a problem with the request.
if(status!=201)
    return null;
       
EntityUtils.consume( response.getEntity() );


--
Thanks.

Alain Fernandez del Toro

unread,
Aug 24, 2016, 11:45:18 AM8/24/16
to Sakai Development
Any way of doing this without the reuse of the context?

Like passing the sessionid as a parameter, header or something?

Matthew Jones

unread,
Feb 21, 2017, 1:51:00 PM2/21/17
to Alain Fernandez del Toro, Sakai Development
Yeah you should be able to use the parameter sakai.session

--
You received this message because you are subscribed to the Google Groups "Sakai Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sakai-dev+...@apereo.org.
To post to this group, send email to saka...@apereo.org.

Adam Marshall

unread,
Feb 21, 2017, 3:10:59 PM2/21/17
to Matthew Jones, Alain Fernandez del Toro, Sakai Development

You could use oauth?

Adam

Get Outlook for Android

Reply all
Reply to author
Forward
0 new messages