0

I have an application, which uses com.microsoft.graph.requests.GraphServiceClient (microsoft-graph artifact version 5.28.0) for downloading files from SharePoint. I construct my GraphServiceClient instance with a pfx certificate through ClientCertificateCredentialBuilder.

new ClientCertificateCredentialBuilder().
  .tenantId(<tenant_id>)
  .clientId(<client_id>)
  .tokenCachePersistenceOptions(<options>)
  .pfxCertificate(getClass().getClassLoader().getResource(<certificate_path>).getPath(), <certificate_password)
  .build();

I then pass those credentials into TokenCredentialAuthProvider with the list of scopes and use it to build my client.

I then use the client to download the content of the file through its sharing URL.

String encodeUrl = getEncodedUrlForShare(sharingUrl);

InputStream content = graphClient.shares(encodedUrl)
  .driveItem()
  .content()
  .buildRequest()
  .get();

Unfortunately, I'm intermittently getting this error when trying to download an existing file:

(ClientCertificateCredential) - Azure Identity => getToken() result for scopes [https://graph.microsoft.com/.default]: SUCCESS
com.microsoft.graph.logger.DefaultLogger logError
SEVERE: CoreHttpProvider[sendRequestInternal] - 408Graph service exception
com.microsoft.graph.logger.DefaultLogger logError
SEVERE: Throwable detail: com.microsoft.graph.http.GraphServiceException: Error code: unauthenticated
Error message: The caller is not authenticated

GET <link>
SdkVersion : graph-java/v5.28.0
401 :
[...]

[Some information was truncated for brevity, enable debug logging for more details]

What's interesting is that when I try to download the file A.pdf, it fails with this error and if I try it once again immediately after the error, it's successful.

My app has Read-Write permissions for the sites, where my files are located.

I tried to set Thread.sleep, but it didn't help - still the first attempt is unsuccessful.

I'm also thinking about repeating this request until it succeeds, but want to know, what's the root cause and if there's any better solution.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45

0 Answers0