LargeFileUploadTask<AttachmentItem># uploadAsync() and upload() fails with "InvalidAudience" error

Copper Contributor

We are working on the Graph API client app 'Send Email'. In order to attach a large file to an email message, I would like to use uploadAsync() or upload() method in the LargeFileUploadTask<AttachmentItem> class, instead of AttachmentFile#contentBytes().

AttachmentFile#contentBytes() method works fine except it is not memory-efficient.

We would like to stream data in uploading the attachment files instead of using the byte array.

The following code was tested with microsoft-graph Java SDK 5.64.0. It failed with

"Error code: InvalidAudience Error message: The audience claim value is invalid ..."

Also "401 : Unauthorized" can be seen in the stacktrace.

It seems that microsoft-graph tried to upload the file content to URL while the access token was for URLs.


Long fileSize =7008981;
AttachmentItem attachItem = new AttachmentItem();
attachItem.attachmentType = AttachmentType.FILE; = "";
attachItem.size = fileSize;
UploadSession session = userRequestBuilder.messages(
InputStream stream = readFileInputStream(path); // get input stream from the attachment file
LargeFileUploadTask<AttachmentItem> chunkUpload =
new LargeFileUploadTask<>(session, graphClient, stream,
fileSize, AttachmentItem.class);
CompletableFuture<LargeFileUploadResult<AttachmentItem>> task;
try {
task = chunkUpload.uploadAsync(chunkSize, null, callback);
largeFileUploadTasks.add(task); // add the task to a list of CompletableFuture objects
} catch (IOException e) {
// Error Handling
} finally {

// After all the attachment file upload tasks are submitted

try {
if (CollectionUtils.isNotEmpty(largeFileUploadTasks)) {
} catch (CompletionException e) {
// Error Handling


0 Replies