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 (https://apis.live.net/v5.0/) you need to register your app. This can be done via the OneDrive Developer Dashboard which at the moment can be found at https://account.live.com/developers/applications/. 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.

Authorizing

First you need to get an authorization code (via https://login.live.com/oauth20_authorize.srf). 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:

https://login.live.com/oauth20_authorize.srf?client_id=0000000123ABCD&scope=wl.signin%20wl.basic%20wl.offline_access%20wl.skydrive_update&response_type=code&redirect_uri=https://login.live.com/oauth20_desktop.srf

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:

https://login.live.com/oauth20_desktop.srf?code=123abc456-12ab-1234-1234-1234abcd1234&lc=1043

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: https://addons.mozilla.org/nl/firefox/addon/restclient/)  to make a GET-call (to https://login.live.com/oauth20_token.srf) 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)
redirect_uri: https://login.live.com/oauth20_desktop.srf

https://login.live.com/oauth20_token.srf?client_id=0000000123ABCD&client_secret=abcdefghzyxwvutsr12345&code=123abc456-12ab-1234-1234-1234abcd1234&grant_type=authorization_code&redirect_uri=https://login.live.com/oauth20_desktop.srf

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 https://login.live.com/oauth20_token.srf) 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
redirect_uri: https://login.live.com/oauth20_desktop.srf

https://login.live.com/oauth20_token.srf?client_id=0000000123ABCD&refresh_token=a_very_long_refresh_token_of_hunderds_of_characters&grant_type=refresh_token&redirect_uri=https://login.live.com/oauth20_desktop.srf

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.

References:

OneDrive signing in with REST – http://msdn.microsoft.com/en-us/library/dn659750.aspx
OneDrive scopes – http://msdn.microsoft.com/en-us/library/dn631845.aspx
OneDrive OAuth 2.0 – http://msdn.microsoft.com/en-us/library/dn631818.aspx
OneDrive Windows desktop apps – http://msdn.microsoft.com/en-us/library/dn631817.aspx
OneDrive development – http://msdn.microsoft.com/en-us/library/dn641952.aspx

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 Step1InitToken.java. 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 *