Using SkyDrive with Codenvy

Create a SkyDrive API app off a Codenvy Factory:

 


SkyDrive (officially Microsoft SkyDrive, previously Windows Live SkyDrive and Windows Live Folders) is a file hosting service that allows users to upload and sync files to a cloud storage and then access them from a Web browser or their local device. It is part of the Windows Live range of online services and allows users to keep the files private, share them with contacts, or make the files public. Publicly shared files do not require a Microsoft account to access.

In this Tutorial we are going to create an Example to get the file information in skydrive using REST API.

Access SkyDrive REST API

  1. User  needs to register an account at http://msdn.microsoft.com/en-us/live/ff519582
  2. After signing in ,Click on the MyApps menu on the top of the page.
  3. Provide the Application Name and click “I accept”
    sky
  4. In the next screen provide the redirect domain and click on save
    clientid
    sky1
  5. Next we need an access_token to consume the REST API
  6. access_token will be received by accessing the below URLhttps://login.live.com/oauth20_authorize.srf?client_id=000000004010B633&scope=wl.skydrive,wl.skydrive_update&response_type=token&redirect_uri=http://stuffjava.blogspot.com/

change client_id,redirect_uri according to the registered application.

7. This url directs the user to authentication page as below
auth
After giving the credentials and clicking on sign In button user will be displayed with a page which asks for application.

Click on yes and the page will redirect to redirect URL provided at the time of application creation.

Access_token can be accessed from the browser url. Complete url  is as below.

http://stuffjava.blogspot.in/#access_token=EwAwAq1DBAAUGCCXc8wU/zFu9QnLdZXy%2bYnElFkAAUTQPQW0gOq5B26SN6rFdAySEDciMPUMXjD2CPrMu3PwLsXOjHaq58Vv%2byXv0Gz/KylJ4zGRR2A2l0paV3B60mxlA4XLMy5MdVuIgzowzhQWL3P83RexZ2HqhNB/rHzSw5k/CJPfHHl6PLeb4hjOtLbrM2yAIeMwtOQFDa%2b1OM3er2xudGUF2e7qdrb6llEkegYn3sNnpoIDej2BcpvWDQmiMH1tAvM4DSOYGp0jP%2bzZRBqwA52L26pRt%2bHnFbhBrjxDfufpDc8OJA64oCay4ii6I6OuAc4v6tKeVDacs5I0Zl4AcIY6DUE2ozG4xBhk87mSxotgvFVfhp4mZWyZt4gDZgAACNM/fD7SgInYAAEfksx7JKJDWtW8ia7C/cNhAjiJ3DHKxwaMFvAlvxjsFVycmnq/6Se54OIOdTPDbrrhAKTRCxVQsgsr1YhxTEWc7miSOGM2evM%2brUXizhud/fC2xrq6XVHUr9pAeebj1AIReTMGp2uI188hhDMCcx7f9lfnmW5w9RtUT3GyMELhhrQuD8h1BOzuqT10JMn5uGpCcYx8atIxwYYNkcIXWBMpAfv2wUc0QkfOxrCRWDsYMy0dczM6h5h7CCoUsRJWBA1OrbMElD5S7xfBfM0TU%2bs4LvMt9HTpg5E%2blATOEkW5WMT9pZ60mwq3RWGN/KDCOPX%2bhnPPVu08Yf/YLlUcDKR3AAA%3d&authentication_token=eyJhbGciOiJIUzI1NiIsImtpZCI6IjEiLCJ0eXAiOiJKV1QifQ.eyJ2ZXIiOjEsImlzcyI6InVybjp3aW5kb3dzOmxpdmVpZCIsImV4cCI6MTM4MzI1MTc3MSwidWlkIjoiYzE5MzJlNjBhMDhmMzZkYTdkYTZlYjIwZmYwZGJkYjQiLCJhdWQiOiJzdHVmZmphdmEuYmxvZ3Nwb3QuY29tIiwidXJuOm1pY3Jvc29mdDphcHB1cmkiOiJhcHBpZDovLzAwMDAwMDAwNDgxMDlBNjEiLCJ1cm46bWljcm9zb2Z0OmFwcGlkIjoiMDAwMDAwMDA0ODEwOUE2MSJ9.ld8xfH9fLCA44-GAbNcGn3FysGTeYiwoVzM8gYQbW1k&token_type=bearer&expires_in=3600&scope=wl.skydrive%20wl.skydrive_update

8. Now we have the access_token value, next we are going to call the REST API to get SKYDrive file information

REST API url :https://apis.live.net/v5.0/me/skydrive/files?access_token==ACCESS_TOKEN

the above URL returns the response in JSON format.

Sample response :

{
"data": [
{
"id": "folder.314b74c89973bea8.314B74C89973BEA8!106",
"from": {
"name": "Anil Etagowni",
"id": "314b74c89973bea8"
},
"name": "Documents",
"description": "",
"parent_id": "folder.314b74c89973bea8",
"size": 27738,
"upload_location": "https://apis.live.net/v5.0/folder.314b74c89973bea8.314B74C89973BEA8!106/files/",
"comments_count": 0,
"comments_enabled": false,
"is_embeddable": true,
"count": 1,
"link": "https://skydrive.live.com/redir.aspx?cid=314b74c89973bea8&page=view&resid=314B74C89973BEA8!106&parid=314B74C89973BEA8!103",
"type": "folder",
"shared_with": {
"access": "Just me"
},
"created_time": "2013-10-30T16:36:49+0000",
"updated_time": "2013-10-30T18:44:30+0000",
"client_updated_time": "2013-10-30T16:36:49+0000"
}, {
"id": "folder.314b74c89973bea8.314B74C89973BEA8!104",
"from": {
"name": "Anil Etagowni",
"id": "314b74c89973bea8"
},
"name": "Pictures",
"description": "",
"parent_id": "folder.314b74c89973bea8",
"size": 151102,
"upload_location": "https://apis.live.net/v5.0/folder.314b74c89973bea8.314B74C89973BEA8!104/files/",
"comments_count": 0,
"comments_enabled": false,
"is_embeddable": true,
"count": 1,
"link": "https://skydrive.live.com/redir.aspx?cid=314b74c89973bea8&page=view&resid=314B74C89973BEA8!104&parid=314B74C89973BEA8!103",
"type": "album",
"shared_with": {
"access": "Just me"
},
"created_time": "2013-10-30T16:36:48+0000",
"updated_time": "2013-10-30T18:42:25+0000",
"client_updated_time": "2013-10-30T16:36:48+0000"
}, {
"id": "folder.314b74c89973bea8.314B74C89973BEA8!105",
"from": {
"name": "Anil Etagowni",
"id": "314b74c89973bea8"
},
"name": "Public",
"description": "",
"parent_id": "folder.314b74c89973bea8",
"size": 32871,
"upload_location": "https://apis.live.net/v5.0/folder.314b74c89973bea8.314B74C89973BEA8!105/files/",
"comments_count": 0,
"comments_enabled": true,
"is_embeddable": true,
"count": 2,
"link": "https://skydrive.live.com/redir.aspx?cid=314b74c89973bea8&page=view&resid=314B74C89973BEA8!105&parid=314B74C89973BEA8!103",
"type": "folder",
"shared_with": {
"access": "Public"
},
"created_time": "2013-10-30T16:36:48+0000",
"updated_time": "2013-10-30T18:49:03+0000",
"client_updated_time": "2013-10-30T16:36:48+0000"
}
]
}

Codenvy Integration includes:

  • create a web application with below components
  • create a servlet to call the REST API and returns the JSON object as String
  • create two jsp file one is for calling the servlet and the second is for showing the json response/li<>

    Create SkyDriveServlet.java

    package com.codenvy;
    import org.apache.http.Header;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.BasicResponseHandler;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;import java.io.IOException;import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class SkyDriveServlet extends HttpServlet{private static final long serialVersionUID = 1L;
    private HttpClient client = new DefaultHttpClient();protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    
    StringBuffer sb = null;
    try {
    String getURL = "https://apis.live.net/v5.0/me/skydrive/files?access_token=EwAwAq1DBAAUGCCXc8wU/zFu9QnLdZXy%2bYnElFkAAb5MB7gcpq87q7JWAa1qtuqam%2bPGkadq59aIAAbhz4BbHloqLHQBLFhxW/ri6b6nn8m5uQ3z7MUDKTgZQwg4og7fnO4oc1qvOdKEFW3qEwt3xuYD8O%2b3bvMHYmb3bHYZlt8lWOMaa2O9T4ToLkuAdnLm7yvqDDAAwunJduFPxYEJyklybFAuwVhtZljEzfaHhU9Nnh%2bu4Mjg3Lnd94%2b1SGk7hbdU1FZj9onyhGl%2brRyKosiJHRgrv1CVKfUayNn3CB4H9AgUR%2bDyvzv6ZlCASZuRPwoeLluoDjXxdUV9yXMCWxvFbRuVG2uLR7U/lpt3%2bfcJmEUYiYHcwF7Q%2bwYRtawDZgAACDVmBQWPWybrAAE2No70%2bLKToPITrT%2bSKCVF1djcfvITivX7T1SglBEy7xIVWBN9nmUsXCo7UAleG4sAsC4xHn%2bp37BtzibunpD%2bv87Q5tcvlBDyxl5JR8O92p6meACVRKoUJVwkEp0S0lP1nvCeogxKp%2buX1epUC9UIxEDg6OpnL9TcZzZhPfu31K4CxdM5UtH9DaMdrgfAxCx1qfZK%2b/Vds%2bzwJzkdqyTlbPUb3V93V5lsMoxpuG2KesorHNyxi18lnxZXg4%2bxnaH6NLjbfCH%2bt8Xu%2bLkLEu5zESv%2bHvztz4Ms16k2G6OwwfZ4o/uLvcdx7vz79MTjIDKD4HMOApCjmPeyDaijLw8iAAA%3d";
    
    HttpGet req = new HttpGet(getURL);
    HttpResponse res = client.execute(req);
    HttpEntity entity = res.getEntity();
    String responseString = new BasicResponseHandler().handleResponse(res);
    if (entity != null) {
    EntityUtils.consume(entity);
    }
    
    sb = new StringBuffer();
    
    sb.append("--------------------------------------------------------------------").append("
    ");
    sb.append(" SkyDrive API REST URL : ").append("
    ");
    sb.append("--------------------------------------------------------------------").append("
    ");
    sb.append("
    "+ getURL +"
    
    ").append("
    ");
    
    sb.append("--------------------------------------------------------------------").append("
    ");
    sb.append(" JSON Response ").append("
    ");
    sb.append("--------------------------------------------------------------------").append("
    ");
    String[] str=responseString.split(",");
    for(String s:str){
    sb.append(s).append("
    ");
    }
    
    } catch (Exception ex) {
    sb = new StringBuffer(ex.toString());
    }
    
    request.setAttribute("data", sb.toString());
    RequestDispatcher rd = request.getRequestDispatcher("/success.jsp");
    rd.forward(request, response);
    }
    }

    Create Index.jsp

    <%  response.sendRedirect("/skydrive");
    %>
    
    

    Create success.jsp

    <html>
    <body>
    <div>
    <p> <br/><%=request.getAttribute("data") %></p>
    <br/>
    </div>
    </body>
    </html>
    
    

    Create web.xml

    <!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd" >
    <web-app>
    <display-name>Hubspot Servlet App</display-name>
    <servlet>
    <servlet-name>skydrive</servlet-name>
    <servlet-class>com.codenvy.SkyDriveServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>skydrive</servlet-name>
    <url-pattern>/skydrive</url-pattern>
    </servlet-mapping>
    </web-app>
    
    

    Dependencies Required in pom.xml

    <dependencies>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    </dependency>
    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2.5</version>
    </dependency>
    <dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version>6.0</version>
    </dependency>
    </dependencies>