singrass
singrass

Reputation: 73

Accessing OneNote API from Java

I am interested in writing a Java application that can access my OneNote notebooks via the OneNote API. I am not sure how to gain access to that API from within Java. Can anybody point me to an example of how to get started here? I use Eclipse as my development environment.

Upvotes: 2

Views: 4078

Answers (3)

lvr123
lvr123

Reputation: 584

This as straightforward process.

The 3 steps would be:

1) create a OneNote application on the OneNote developper's page. More info here https://dev.onedrive.com/app-registration.htm. This is a one time action.

2) your java application should then provide an authentification mechanism and a tolken-refresh mechanism.

See this post for more info on the authentification mechanism part : Getting a OneNote token with Java. This post is about the OAuth 2.0 flow 'Authorization code grant flow'. More info here https://msdn.microsoft.com/en-us/library/hh243647.aspx#flows

3) your java application calls adhoc API Rest methods to retreive the needed informations.

Example to retrieve all your notebooks (using OkHttp for Http requests):

private final static String NOTEBOOKS_ENDPOINT = "https://www.onenote.com/api/v1.0/me/notes/notebooks";


public Notebooks readAllNoteBooks() {

    try {
        if (client == null)
            client = new OkHttpClient();
        Request request = createOneNoteRequest(a_valid_tolken, NOTEBOOKS_ENDPOINT);
        Response response = client.newCall(request).execute();
        JsonObject content = UrlHelper.parseResponse(response);
        System.out.println(content);

        return Notebooks.build(content.get("value"));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

public static Request createOneNoteRequest(String mAccessToken, String url) {

    Request.Builder reqBuilder = new Request.Builder();
    reqBuilder.url(url);
    reqBuilder.header("Authorization", "Bearer " + mAccessToken);

    return reqBuilder.build();

}

NoteBooks and NoteBook are 2 tiny classes matching the key attributes from the OneNote objects.

Upvotes: 1

JamesLau-MSFT
JamesLau-MSFT

Reputation: 284

singrass,

In addition to the above replies, the Android OneNote API sample may also help you. There is no OneNote application class that you can create (unless you want to create your own). You simply call the API through the HttpClient. If you are unfamiliar on how to call REST APIs in Java in general, this thread may help you.

-- James

Upvotes: 0

Adi
Adi

Reputation: 2394

Microsoft has provided REST apis for accessing One note functionalities like creating and accessing notes. See OneNote Rest API reference.

Upvotes: 0

Related Questions