SharePoint The caller is not authenticated intermittently for Java GraphServiceClient

Daniel Polách 1 Reputation point

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

new ClientCertificateCredentialBuilder().
.pfxCertificate(getClass().getClassLoader().getResource(<certificate_path>).getPath(), <certificate_password)

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 file through its sharing URL.

String encodeUrl = getEncodedUrlForShare(sharingUrl);

InputStream content = graphClient.shares(encodedUrl)

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

(ClientCertificateCredential) - Azure Identity => getToken() result for scopes []: SUCCESS logError
SEVERE: CoreHttpProvider[sendRequestInternal] - 408Graph service exception logError
SEVERE: Throwable detail: 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 error, it's successful.

My app has Read-Write permissions for the sites, in which 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.

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
1,981 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 20,061 Reputation points Microsoft Vendor

    Hi @Daniel Polách
    Please make sure that your application is presenting a valid access token to Microsoft Graph as part of the request. This error often means that the access token may be missing in the HTTP authenticate request header or that the token is invalid or has expired. We strongly recommend that you use the Microsoft Authentication Library (MSAL) for access token acquisition. Additionally this error may occur, if you try to use a delegated access token granted to a personal Microsoft account, to access an API that only supports work or school accounts (organizational accounts).

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.