Authenticating for MS OneDrive

Viewed 5772

I am working on a Spring-MVC application in which I would like to integrate OneDrive functionality. Unfortunately, I am not able to find some SDK which I can use for a webapp. Something in Maven repository will help. Can anyone tell me how I can authenticate and use some libraries for OneDrive.

Isn't there any SDK which I can directly put in POM.xml, like Google, dropbox and many other services have.

Here are some of the links I have tried:

  1. https://msdn.microsoft.com/en-us/library/hh826531.aspx

  2. http://www.tjeerd.net/2014/08/23/onedrive-api-restful-programming-in-java/

  3. https://dev.onedrive.com/sample-code.htm

Samples in above are for android, nothing covers webapp. Thank you.

2 Answers

After almost five years I'd like to add that there is a Java SDK for MS Graph which will also cover your OneDrive access: https://github.com/microsoftgraph/msgraph-sdk-java

Probably you will already have solved your issues but for references. Add this to your POM:

<dependency>
  <groupId>com.microsoft.graph</groupId>
  <artifactId>microsoft-graph</artifactId>
  <version>2.7.1</version>
</dependency>

And you can for example get all your files in the OneDrive like that:

Builder2 clientBuilder = GraphServiceClient.builder()
    .authenticationProvider(_accessTokenAuthProvider);

IHttpProvider httpProvider = DefaultClientConfig
    .createWithAuthenticationProvider(_accessTokenAuthProvider)
    .getHttpProvider(createOkHttpClientBuilder().build());
clientBuilder.httpProvider(httpProvider);

IGraphServiceClient graphServiceClient = clientBuilder.buildClient();
IDriveItemCollectionPage collectionPage = graphServiceClient.me().drive().root().children().buildRequest()
        .select("Id,Name,Folder,Size").get();

In the code snippet above, you would need to add the AuthenticationProvider which will implement IAuthenticationProvider, ICoreAuthenticationProvider. On executing you will get the collectionPage which will contain a list of DriveItems.

Hope this helps others.

Related