Forum Discussion

Aravind_Snaplogic's avatar
Aravind_Snaplogic
Copper Contributor
Jul 25, 2023

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

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 https://outlook.office365.com URL while the access token was for https://graph.microsoft.com URLs.

 

Long fileSize =7008981;
AttachmentItem attachItem = new AttachmentItem();
attachItem.attachmentType = AttachmentType.FILE;
attachItem.name = "yourAttachmentFilename.zip";
attachItem.size = fileSize;
UploadSession session = userRequestBuilder.messages(message.id).attachments()
.createUploadSession(AttachmentCreateUploadSessionParameterSet.newBuilder()
.withAttachmentItem(attachItem).build()).buildRequest().post();
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 {
IOUtils.closeQuietly(stream);
}

// After all the attachment file upload tasks are submitted

try {
if (CollectionUtils.isNotEmpty(largeFileUploadTasks)) {
largeFileUploadTasks.forEach(CompletableFuture::join);
}
} catch (CompletionException e) {
// Error Handling
}

 

No RepliesBe the first to reply

Resources