How to customize Microsoft Graph client using OkHttpClient

Sujit Jadhav 1 Reputation point
2021-03-14T06:16:56.253+00:00

Im using client credential approach to fetch data using Microsoft Graph api. As mentioned in documentation, we can customize it.

Below is the code snippet i tried:

final ClientCredentialProvider authProvider = new ClientCredentialProvider(this.clientId, this.scopes, this.clientSecret, this.tenantId, NationalCloud.Global);
final OkHttpClient httpClient = HttpClients.createDefault(authProvider).newBuilder().build();
final IHttpProvider httpProvider = DefaultClientConfig.createWithAuthenticationProvider(authProvider).getHttpProvider(httpClient);
final IGraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();

However im getting below errors:
2021-03-12T22:10:39,340 ERROR [restartedMain] c.m.g.l.DefaultLogger: CoreHttpProvider[send] - 220Graph service exception Error code: InvalidAuthenticationToken
2021-03-12T22:10:39,340 ERROR [restartedMain] c.m.g.l.DefaultLogger: CoreHttpProvider[send] - 220Error message: CompactToken parsing failed with error code: 80049217
2021-03-12T22:10:39,340 ERROR [restartedMain] c.m.g.l.DefaultLogger: CoreHttpProvider[send] - 220
2021-03-12T22:10:39,341 ERROR [restartedMain] c.m.g.l.DefaultLogger: CoreHttpProvider[send] - 220GET https://graph.microsoft.com/v1.0/users?%24select=Id%2CDisplayName%2CMail%2CUserPrincipalName%2CEmployeeId&%24expand=registeredDevices
2021-03-12T22:10:39,341 ERROR [restartedMain] c.m.g.l.DefaultLogger: CoreHttpProvider[send] - 220SdkVersion : graph-java/v2.6.0
2021-03-12T22:10:39,341 ERROR [restartedMain] c.m.g.l.DefaultLogger: CoreHttpProvider[send] - 220Authorization : [PII_REDACTED]
2021-03-12T22:10:39,341 ERROR [restartedMain] c.m.g.l.DefaultLogger: CoreHttpProvider[send] - 220
2021-03-12T22:10:39,342 ERROR [restartedMain] c.m.g.l.DefaultLogger: CoreHttpProvider[send] - 220
2021-03-12T22:10:39,342 ERROR [restartedMain] c.m.g.l.DefaultLogger: CoreHttpProvider[send] - 220401 : Unauthorized
2021-03-12T22:10:39,342 ERROR [restartedMain] c.m.g.l.DefaultLogger: CoreHttpProvider[send] - 220[...]
2021-03-12T22:10:39,342 ERROR [restartedMain] c.m.g.l.DefaultLogger: CoreHttpProvider[send] - 220
2021-03-12T22:10:39,343 ERROR [restartedMain] c.m.g.l.DefaultLogger: CoreHttpProvider[send] - 220[Some information was truncated for brevity, enable debug logging for more details]
2021-03-12T22:10:39,343 ERROR [restartedMain] c.m.g.l.DefaultLogger: Throwable detail: com.microsoft.graph.http.GraphServiceException: Error code: InvalidAuthenticationToken
Error message: CompactToken parsing failed with error code: 80049217

GET https://graph.microsoft.com/v1.0/users?%24select=Id%2CDisplayName%2CMail%2CUserPrincipalName%2CEmployeeId&%24expand=registeredDevices
SdkVersion : graph-java/v2.6.0
Authorization : [PII_REDACTED]

401 : Unauthorized
[...]

If i remove the client customization code it works. Below code is working:

final ClientCredentialProvider authProvider = new ClientCredentialProvider(this.clientId, this.scopes, this.clientSecret, this.tenantId, NationalCloud.Global);
final IGraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();

Im unable to find what is causing issues when trying to customize the Client.

Any help will be greatly appreciated.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,338 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Andre Haverdings 6 Reputation points
    2022-10-04T22:12:30.57+00:00

    you need to use HttpClients.createDefault(authProvider) without the .newBuilder().build() or use:

    HttpClients.custom()  
                    .addInterceptor(new AuthenticationHandler(auth))  
                    .addInterceptor(new RetryHandler())  
                    .addInterceptor(new RedirectHandler())  
                    .build();  
    
    0 comments No comments