SharePoint The caller is not authenticated intermittently for Java GraphServiceClient

Daniel Polách 1 Reputation point
2022-11-14T10:05:55.63+00:00

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 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 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 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.
2,902 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 34,821 Reputation points Microsoft Vendor
    2022-11-15T05:33:57.897+00:00

    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.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.