OneDrive API RESTful programming in Java – part 2: making calls with Java

In my previous tutorial I tried to explain the registration and and authorization of a OneDrive app. So you should have a basic understanding of how the process flow works. In this second tutorial I will try to explain how to make calls to the OneDrive API via Java. The examples are based on the OneDriveAPI which I wrote and made available on GitHub.

Talking RESTful with the Jersey and Jackson framework

To be able to talk with any RESTful service I used the Jersey framework. This is an (I quote) “open source, production quality, framework for developing RESTful Web Services in Java that provides support for JAX-RS APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation.“. In other words this framework makes it easy to program against RESTful services (Jersey-client) or to make RESTful services (Jersey-server). The Jackson framework is used for mapping (or converting) JSON responses to Java beans and vice versa.

Basically all RESTful Java calls to the OneDrive RESTful API look in Jersey like the following:

import com.sun.jersey.api.*;
import com.fasterxml.jackson.databind.ObjectMapper;
MultivaluedMap<String, String> queryParams;
Client jerseyClient;
jerseyClient = Client.Create();
WebResource webResource = client.resource("");
ClientResponse clientResponse =  webResource.queryParams(queryParams).accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);

MyBean myBean = objectMapper.readValue(clientResponse.getEntity(String.class).toString(), MyBean.class);

Where the queryParams are the parameters added to the end of the URL. The Client is our RESTful Jersey client and the resource defines which RESTful URL we are calling. The final step is using the WebResource object to make the real call. We specify the media type which we are accepting from the server via the accept method and after that we specify we are doing a GET call.

When the WebResource has been executed it will return the response in the ClientResponse object. Quite often the result will be returned in JSON format and we can use the Jackson library to map the JSON response from the RESTful service to a Java bean. This is done using the ObjectMapper object which converts the client response entity (a JSON string) to a custom bean (MyBean).

Basically that’s how the Jersey library works. I would suggest to look into the OneDrive API source code to see how all different calls (like GET/POST/PUT/DELETE) work.

The OneDrive API

I put together some basic RESTful calls for the OneDrive RESTful service into a Maven based project called OneDrive API for Java. The following basic functionalities are implemented:

  • list folders and/or folder contents
  • create/delete/update folder
  • change file properties
  • upload/download files
  • get owner/user
  • get shared edit/read link from a file

The project contains a readme which explains in three steps how to get started with your first application. The project is open source so please feel free to contribute, fork or give feedback for improvement.

3 Comments Posted in /dev


  1. I really appreciate this tutorial it was really awesome, helpful and easy to follow. but i have some question to ask because i haven’t seen how to use it with my project.

    After i completed all the required step that needed to be followed, i run the code the output was that:{“error”:”invalid_grant”,”error_description”:”The provided value for the ‘code’ parameter is not valid. The code has expired.”}, then after that line: “Refresh token null”.

    And i already did the third step in the github before i run the code.
    Do i need to used the refresh token i got from the web browser in the or not?
    how do i connect it with my application?
    What should i have already before i can connect this OneDrive API tutorial?
    please help me

  2. I was trying to run your code and i see client secret is needed now in fetching the access_token from refresh_token.
    API returning authentication_token along with access_token and seeing the parsing issue.

    I fixed both locally and working fine.

    Can you plz update the same in your code and commit on github.

  3. I really appreciate this tutorial it was really awesome, helpful and easy to follow. but i have some question to ask because i haven’t seen how to use it with my project.

    How can I upload a file to specific folder of onedrive?

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>