Api - Initialize API Connection

Initialize API Connection

All API Communications are authenticated through two session cookies that need to be sent in each API call to the site by the client making the communication.

  • SESSION_ID - contains randomly assigned id for the current session
  • SESSION_INFO - contains authenticated token

The cookies once obtained from the site are held by the client during the entire duration of the current session. These cookies are obtained by making an initial call to the site (as shown in the code snippet below) by sending the API username and password in the request parameters.

To get the first cookie (SESSION_ID)

Send a HTTP request to URL http://yoursite.edubrite.com/oltpublish/site/connect.do

No parameters need to be sent (they are ignored)

To get the second cookie (SESSION_INFO)

Send a HTTP request to URL http://yoursite.edubrite.com/oltpublish/site/connect.do with SESSION_ID cookie (in the header) and following parameters

  • username - this should be set to api username
  • password - this should be set to api password

If the authentication is successful, you will be able to get SESSION_INFO cookie from the response headers. Once you have these two cookies, you must save them (in variables) and set them in all subsequent requests. Do not create new session for each API calls (there is a throttling limit placed on number of API sessions per microsite).

 

Check the API Setup document to know more about how to enable integration in the microsite and obtain the API UserName and password.

A reference implementation of a client library in java is available for reference. All references from the code snippets are made to this library. To check PHP reference look up here

 

Code Snippet

Call to Connect and Obtain Cookies

The following code snippet would make the initial connections to the site and receive the two set of cookies using API username/password authentication. On successful connect (as indicated by the success flag), the client should keep holding the Connection object throughout their entire API session. Connection object internally stores the two cookies. This Connection object can then be used to make all the API calls.

 

ConnectionConfig config = ConnectionConfigManager.getInstance().getConnectionConfig();
Connection connection = new Connection(config);
boolean success = connection.connect();
if (success) {
     String sessionId = connection.getSessionId();
     String sessionInfo = connection.getSessionInfo();
} else {
     CommunicationError error = connection.getLastError();
}   
String responseXml = connection.getLastResponseXml();
int httpCode = connection.getLastHTTPCode();

 

Disconnect and Release Cookies

Before ending the API session, simply call the disconnect method on the Connection object which was obtained during the initial call as shown above. No API call would be successful on the Connection object once the disconnect method has been called.

 

connection.disconnect();

 

Response

1. Correct combination of username and password specified

     HTTP Response Code = HttpServletResponse.SC_OK

     Response XML

<response code="0">
    <msgs>
         <msg>
             <code>0152</code>
             <value><![CDATA[API Connection Successful]]></value>
         </msg>
    </msgs>
</response>

 

2. Invalid username/password combination specified

     HTTP Response Code = HttpServletResponse.SC_UNAUTHORIZED

     Response XML

<response code="-1">
    <msgs>
         <msg>
             <code>0014</code>
             <value><![CDATA[Invalid username or password.]]></value>
         </msg>
         <msg>
             <code>0154</code>
             <value><![CDATA[API Connection Not Successful]]></value>
         </msg>
    </msgs>
</response>


Rating: