question

SujitJadhav-2935 avatar image
0 Votes"
SujitJadhav-2935 asked saldana-msft edited

How to customize Microsoft Graph client using OkHttpClient

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-sdkmicrosoft-graph-usersmicrosoft-graph-identity
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Adding right tags/teams to assist

0 Votes 0 ·

Hi,
Thanks for reaching out.
Java SDK v3 was released recently and features an improved and simplified authentication experience.
We've also updated the documentation linked in your original post.
Can you upgrade your application to v3, update the code and see if you're still having the same issue please?


0 Votes 0 ·

Hi VincentBiret ,
I'm facing the same issue, though I'm using latest version of Java SDK.
The version used:
microsoft-graph:5.14.0
azure-identity:1.5.0-beta.1

Maven Dependencies :

                 <dependency>
             <groupId>com.microsoft.graph</groupId>
             <artifactId>microsoft-graph</artifactId>
             <version>[5.0,)</version>
         </dependency>
         <dependency>
             <groupId>com.azure</groupId>
             <artifactId>azure-identity</artifactId>
             <version>[1.3,)</version>
         </dependency>




0 Votes 0 ·

0 Answers