I had an application using Graph API in Java that required an IAuthenticationProvider
in order to build the GraphClient.
GraphServiceClient<Request> client = authService
.graphClient(authService.getAuthProvider(authGraphProviderRequest));
My GraphClient is implemented in the service layer:
@Override
public GraphServiceClient<Request> graphClient(IAuthenticationProvider iAuthenticationProvider) {
return GraphServiceClient.builder()
.authenticationProvider(iAuthenticationProvider)
.buildClient();
}
To retrieve the authProvider
, I call getAuthProvider
, and this method requires an AuthGraphProviderRequest
object:
@Builder
@Getter @Setter
@NoArgsConstructor @AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class AuthGraphProviderRequest {
private String tenantClientId;
private String tenantClientSecret;
private String tenantApplicationId;
private String tenantAuthority;
private String scope;
private String token;
}
This class can either send token OR the clientId
, clientSecret
, applicationId
and scope to get an Authentication provider.
@Override
public IAuthenticationProvider getAuthProvider(AuthGraphProviderRequest authGraphProviderRequest) throws Exception {
if (authGraphProviderRequest.getToken() == null)
authGraphProvider.setToken(authenticate(authGraphProvider).accessToken());
return requestUrl -> {
CompletableFuture<String> authProvider = new CompletableFuture<>();
authProvider.complete(authGraphProvider.getToken());
return authProvider;
};
}
For this implementation I was using the following dependencies:
<!-- https://mvnrepository.com/artifact/com.microsoft.graph/microsoft-graph-core -->
<dependency>
<groupId>com.microsoft.graph</groupId>
<artifactId>microsoft-graph-core</artifactId>
<version>2.0.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/msal4j -->
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>msal4j</artifactId>
<version>1.13.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.graph/microsoft-graph -->
<dependency>
<groupId>com.microsoft.graph</groupId>
<artifactId>microsoft-graph</artifactId>
<version>5.57.0</version>
</dependency>
I have to update the following dependencies with:
<dependency>
<groupId>com.microsoft.graph</groupId>
<artifactId>microsoft-graph-core</artifactId>
<version>3.1.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/msal4j -->
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>msal4j</artifactId>
<version>1.17.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.graph</groupId>
<artifactId>microsoft-graph</artifactId>
<version>6.14.0</version>
</dependency>
Apparently, IAuthenticationProvider
has been removed and replaced with AuthenticationProvider
, which is implemented by BaseBearerTokenAuthenticationProvider
, for example.
My issue is that I don't find how I can retrieve the Provider for my client.
I wanted to use token because i thought it wasn't cached. Actually, I just needed to get a ClientCredential and instantiate directly the client. There were no need to use IAuthenticationProvider.
@Override
public GraphServiceClient graphClient(
AuthGraphProviderRequest authGraphProviderRequest) {
ClientSecretCredential clientSecretCredential = clientSecretCredential(authGraphProviderRequest);
List<String> scopes = new ArrayList<>();
if(authGraphProviderRequest.getScope() == null)
scopes.add(SharepointConstants.SCOPE_MS_GRAPH_DEFAULT);
else
scopes.add(authGraphProviderRequest.getScope());
return graphClient(clientSecretCredential, scopes.toArray(String[]::new));
}
@Override
public GraphServiceClient graphClient(
ClientSecretCredential clientSecretCredential,
String[] scopes) {
return new GraphServiceClient(clientSecretCredential, scopes);
}
@Override
public ClientSecretCredential clientSecretCredential(
AuthGraphProviderRequest authGraphProviderRequest) {
return new ClientSecretCredentialBuilder()
.clientId(authGraphProviderRequest.getClientId())
.clientSecret(authGraphProviderRequest.getClientSecret())
.tenantId(authGraphProviderRequest.getTenantId())
.build();
}