Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an object of IAuthenticationProvider of GraphServiceClient with fixed inputs

I am trying to connect to Microsoft Share Point from my Java application. The documentation for Microsoft Graph SDK for Java is not so clear.

I am trying to initiate the Graph client, while providing the credentials needed via a custom GUI or configuration file.

I am trying to do as follow but can

IGraphServiceClient client = GraphServiceClient.builder().authenticationProvider(authenticationProvider).buildClient();

I need the "authenticationProvider" object to be of a class implementing IAuthenticationProvider, however its not clear what parameters to add or how to create this object. Has anyone tried this before and what is the correct way to build the client and provide the required credentials?

like image 642
Selim Alawwa Avatar asked Apr 15 '19 13:04

Selim Alawwa


People also ask

What is GraphServiceClient?

The GraphServiceClient class is used to operate the Microsoft Graph which is not able to get the access_token or refresh_token.

What is Clientcredentialprovider?

public interface AWSCredentialsProvider. Interface for providing AWS credentials. Implementations are free to use any strategy for providing AWS credentials, such as simply providing static credentials that don't change, or more complicated implementations, such as integrating with existing key management systems.

What does Microsoft Graph API do?

The Microsoft Graph API is a RESTful web API that enables you to access Microsoft Cloud service resources. After you register your app and get authentication tokens for a user or service, you can make requests to the Microsoft Graph API.


2 Answers

Microsoft has an example project where they have a simple instance of IAuthenticationProvider.

public class SimpleAuthProvider implements IAuthenticationProvider {

    private String accessToken = null;

    public SimpleAuthProvider(String accessToken) {
        this.accessToken = accessToken;
    }

    @Override
    public void authenticateRequest(IHttpRequest request) {
        // Add the access token in the Authorization header
        request.addHeader("Authorization", "Bearer " + accessToken);
    }    
}
like image 130
Vyrd Avatar answered Oct 05 '22 19:10

Vyrd


The AuthenticationProviders that implement a variety of different OAuth flows are available in a seperate package. See this Github repo here:
https://github.com/microsoftgraph/msgraph-sdk-java-auth

like image 22
Darrel Miller Avatar answered Oct 05 '22 20:10

Darrel Miller