Microsoft One Drive provides lot of benefits on the cloud by managing the documents from multiple systems and provides integration capabilities with Microsoft software products. 

Here I am going to explain leveraging the benefits of both Liferay Document Library & Microsoft One Drive.

Let’s consider a use case that Liferay portal is implemented as Intranet / Partner / Customer portal where some of the users have the documents in the One Drive.

Now if they want to have one common interface to manage the personal documents in One Drive and official documents in Liferay Document & Media Library then this article contains the detailed implementation & integration of One Drive Repository so that user can have flexibility & features from both the repository.

Develop Hook for Document & Media Library to invoke custom struts action to access the One Drive API.

Now Create the account in the Microsoft One Drive and register the application in the system for ex:- name of the application is LR portal. Now generate the Client ID & Client Secret Key to be shared with Liferay portal in order to authenticate.

 

Please find below the details of integrate with One Drive API:-

 

Client ID:

00000000401XXXXX - this is a unique identifier for your application.

Client secret (v1):

NA1HbvXXXXXXX2zRecXg32yaWfs69Jxh.  For security purposes, don't share your client secret with anyone.

 

Store clientID & secret in the portal-ext.properties file as it will be constant for all our webapp calls. 

 

Required query string parameters to be passed as per below:-

 

Name             Value  Description

client_id         string    The client ID value created for your application.   (00000000401XXXXX)

scope            string     A space-separated list of scopes your application requires. (wl.signin wl.offline_access onedrive.appfolder)

redirect_uri    string    The redirect URL that the browser is sent to when authentication is complete.                              (https://riiteshbhachawat.com/integrations)

 

For Ex:- 

scope:- wl.signin wl.offline_access onedrive.appfolder

 

Token flow

GET https://login.live.com/oauth20_authorize.srf?client_id={client_id}&scope={scope}

    &response_type=token&redirect_uri={redirect_uri}

 

You will get access token as a response from the one drive and then you can make subsequent REST API calls by providing the access_token through HTTP Header. For ex:-

You provide an access token through an HTTP header:-

Authorization: bearer {token}

 

Response

Upon successful authentication and authorization of your application, the web browser will be redirected to your redirect URL with additional parameters added to the URL.

 

https://login.live.com/oauth20_authorize.srf#access_token=EwC...EB

  &authentication_token=eyJ...3EM&token_type=bearer&expires_in=3600

  &scope=onedrive.readwrite&user_id=3626...1d

 

Values for access_token, authentication_token, and user_id are truncated in the previous example. The values for access_token and authentication_token are quite long.

You can use the value of access_token to make requests to the OneDrive API.

 

One Drive:-

Get user's default Drive metadata   GET /drive

https://api.onedrive.com/v1.0/GET /drive

LR needs to pass the Access Token along with GET request to One Drive API in order to get the response from the One Drive Server.

 

Now to get the specific user data through REST API, below is the sample of the call:-

 

GET https://apis.live.net/v1.0/ritesh@coarys.com?access_token=Authorization: bearer EwBwAq1DBAAUGCCXc8wU/zFu9QnLdZXy+YnElFkAAfhaFv0WOrA5dZ30IlgqQl+Fu2b5GzVSImo0hTflXN54xfJuu8ar1H1M+

 

Each client in portal will have the unique Access token to their Drive objects. Below diagram depicts the process to get the objects from One Drive:- 

 

 

 

 

 

 

Sequence Diagram: -

The below Sequence diagram explains the GET & POST  calls to obtain Tokens & Resources using OAuth & Rest API -

 

 

 

 

 

The flow of integration at the API level is mentioned below: -

 OneDrive uses OAuth 2.0 for authentication. LR will get an access token to authenticate with a particular set of permissions for a LR user. LR will provide an access token through an HTTP header:

                                                                    Authorization: bearer {token}

 The OneDrive API exposes below resource types: 

  • Drive – Drive for the user.
  • Item – files, folders.

 

1)  Root resources

LR will get root resources to access an item or drive.

Path

Resource

/drive

User's default Drive.

/drives

List Drives available to the authenticated user.

/drives/{drive-id}

Access a specific Drive by its ID.

/drives/{drive-id}/root/children

List items in the root of a specific Drive.

/drive/items/{item-id}

Access an Item by its ID.

 

 

2)  Drive resource

The Drive is the top level object within a user's OneDrive. LR user will always have at least one Drive available--the default Drive. 

 Below are the examples to use /drive   along with /drives/ {drive-id} is valid too.

 

Common API details

HTTP method

Get user's default Drive metadata

GET /drive

Get Drive metadata of another Drive

GET /drives/{drive-id}

Get root folder for user's default Drive

GET /drive/root

List children under the Drive

GET /drive/root/children

List changes for all Items in the Drive

GET /drive/root/view.delta

Search for Items in the Drive (preview)

GET /drive/root/view.search

 

 

 

3)   Item resource

Items are the objects inside the OneDrive file system. LR can access the items by their id using the /items/{item-id} syntax, or by their file system path using the /drive/root:/path/to/file syntax. 

Folders act as containers of items, and have a children reference pointing to a collection of items under the folder.   

 

Common task

HTTP method (by ID)

HTTP method (by path)

Get metadata for an Item

GET /drive/items/{id}

GET /drive/root:/{path}