0

I would like to use Microsoft Graph API to manage Drive files, but I have some trouble with authentication...

I have the code below to get the rights to access to my account :

try(InputStream propFile = MicrosoftEngine.class.getClassLoader().getResourceAsStream("microsoft.properties")){
      Properties prop = new Properties();
      prop.load(propFile);

      setGraphClient(GraphServiceClient.builder().authenticationProvider(request -> {
        // Add the access token in the Authorization header
        request.addHeader("Authorization", "Bearer " + prop.getProperty("token"));
      }).buildClient());

      IDriveItemCollectionPage children = graphClient.me().drive().root().children().buildRequest().get();
      setDrive(children);
    }
    catch(IOException e) {
      e.printStackTrace();
    }

But for the moment, the value of the "token" property is taken from Microsoft Graph Explorer :

Example here

After a while (an hour maybe), the token expires and I need to send another request via the Graph Explorer to have a new token and copy/paste it into my Java code.

But this is not convenient... How can I get this token value in Java ? Any ideas ?

Thanks in advance for your help :)

5
  • You might need to make a separate request to request a token. What does the API documentation say about this? Commented Nov 18, 2020 at 15:41
  • For Authentication first you need to implement MSAL to get the token from AAD. You can start from here to implement authentication in Microsoft Graph. You can try this sample to get started with implementation of Microsoft Graph for JAVA. Commented Nov 18, 2020 at 15:43
  • @MLarionov actually this is a code I got from the documentation... Commented Nov 18, 2020 at 16:37
  • @Shiva-MSFTIdentity Thank you, I've tested the sample and I got some results with Calendar data, I'm going to try to acces to my OneDrive files with the authentication part of the sample :) Commented Nov 18, 2020 at 16:39
  • Glad that it helped you. Moving this to Answer. please mark it as the answer by clicking the check mark. Doing so helps others find answers to their questions. See meta.stackexchange.com/questions/5234/… :)- Commented Nov 18, 2020 at 16:42

1 Answer 1

2

For Authentication first you need to implement MSAL to get the token from AAD. You can start from here to implement authentication in Microsoft Graph. You can try this sample to get started with implementation of Microsoft Graph for JAVA.

Sign up to request clarification or add additional context in comments.

2 Comments

Hello again @Shiva-MSFTIdentity, in the sample a DeviceCode is used to acquire the token, and I tried to replace it with IntegratedWindowsAuthenticationParameters, to avoid user sign-in (I use my personnal account). But I got an error : "com.microsoft.aad.msal4j.MsalServiceException: WsTrust endpoint not found in metadata document"... Is there something I should modify in my App Registration ?
Hi @digimip09, You can follow this public document implementing Integrated Windows Authentication. If you have any more issues with Authentication mainly, please create a stack overflow question with tag azure-active-directory so that the Identity team can look into it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.