OneDrive API RESTful programming in Java – part 1: registering and authorizing

You want to connect your Java application to Microsoft’s OneDrive via RESTful? In this tutorial series I will try to explain what’s needed for that. This first tutorial is about setting up OneDrive, the next tutorial will cover the Java implementation.

First you need to sign up for a Microsoft Live account, with this account you can use the OneDrive RESTful API.

Registering your application

To give your application access to the OneDrive RESTful API ( you need to register your app. This can be done via the OneDrive Developer Dashboard which at the moment can be found at Create a new “app” and give it a name “MyApp”. Go to the API-settings and make sure that it’s a “Mobile or desktop” app. In the App-settings overview you will see your client-id and client-secret. Keep these for yourself, you will need them later on. In this tutorial I will use client-id  0000000123ABCD and client-secret abcdefghzyxwvutsr12345.


First you need to get an authorization code (via Replace the client_id parameter with your client-id value. The scope determines what kind of permissions you give to the account.  Separate the scopes by using the %20 sign, for now we use the wl.signin, wl.basic, wl.offline_access and wl.skydrive_update scope. Open the following URL in your browser:

The server will ask your permission to the scopes given in the URL. Confirm with a “Yes”. Look in the address bar for the response code (in this example it’s 123abc456-12ab-1234-1234-1234abcd1234) and write it down somewhere. The response URL will look similar to the following:

Access token – Refresh token

Every call you make to the OneDrive RESTful API you need to send a so called access token, which expires after 3600 seconds. When you also have a refresh token (doesn’t expire) you can refresh your access token continuously. To get an access token and refresh token you need a REST-client (I use a REST-client plugin for Firefox:  to make a GET-call (to with the content type: Content-Type: application/x-www-form-urlencoded and the following parameters:

client_id: 0000000123ABCD (see OneDrive Dashboard for your app client-id)
client_secret: abcdefghzyxwvutsr12345 (see OneDrive Dashboard for your client-secret)
code: 123abc456-12ab-1234-1234-1234abcd1234 (see Authorization chapter for the authorization code)
grant_type: authorization_code (we want to get access and refresh tokens)

The response will be similar like the following:

  "token_type": "bearer",
  "expires_in": 3600,
  "scope": "wl.signin wl.basic wl.offline_access wl.skydrive_update",
  "access_token": "a_very_long_access_token_of_hundreds_of_characters",
  "refresh_token": "a_very_long_refresh_token_of_hunderds_of_characters",
  "user_id": "1234567890abcdefghij"

Getting new access tokens using the refresh token

Now you have your access token and refresh token you can use the refresh token to get a new access token whenever you want. You need to make a GET call (to with the content type: Content-Type: application/x-www-form-urlencoded and the following parameters:

client_id: 0000000123ABCD
refresh_token: a_very_long_refresh_token_of_hundreds_of_characters
grant_type: refresh_token

The server will respond with the following JSON which contains your new access_token:

 "token_type": "bearer",
 "expires_in": 3600,
 "scope": "wl.signin wl.basic wl.offline_access wl.skydrive_update",
 "access_token": "new_very_long_access_token_of_hundreds_of_characters",
 "refresh_token": "a_very_long_refresh_token_of_hundreds_of_characters",
 "user_id": "1234567890abcdefghij"

With these first steps we now have registered a new app in the OneDrive Dashboard, signed in and are able to get our tokens to be able to make calls to the API. In the next part of this series we will make real API calls.


OneDrive signing in with REST –
OneDrive scopes –
OneDrive OAuth 2.0 –
OneDrive Windows desktop apps –
OneDrive development –

3 thoughts on “OneDrive API RESTful programming in Java – part 1: registering and authorizing

  1. Great tutorial. A typo is there. Please correct it: you have mentioned – refresh_token: a_very_long_access_token_of_hundreds_of_characters while you are talking about how to get the new access token. It should be refresh_token: a_very_long_refresh_token_of_hundreds_of_characters

  2. Hi,

    I am very glad to you for this post and I got null for the value of Refresh token when I’m trying to run Please help me .

    The console prints like this:-

    11:56:25.563 [main] INFO n.tjeerd.onedrive.core.OneDriveCore – OneDriveAPI core initialized.
    11:56:25.580 [main] INFO net.tjeerd.onedrive.core.OneDrive – Client-id, client secret and authorization code found, trying to initialize tokens
    oAuth20Token net.tjeerd.onedrive.json.OAuth20Token@55796279
    Refresh token: null

  3. Hello, thank you very much.
    Just to signal that you write to make GET calls for tokens and refresh, but I got error response telling to use POST.
    I did them with POST and then all your requests are functionning !
    Thanks again

Leave a Reply

Your email address will not be published. Required fields are marked *