digimip09
digimip09

Reputation: 13

Get token for Microsoft Graph API in Java

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 :)

Upvotes: 0

Views: 7826

Answers (1)

Shiva Keshav Varma
Shiva Keshav Varma

Reputation: 3575

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.

Upvotes: 2

Related Questions