0

I am trying to use the following code, but am getting "Message: The audience claim value is invalid for current resource. Audience claim is 'https://graph.microsoft.com', request url is 'https://outlook.office.com/api/beta/Users..."

I get it on the provider.GetUploadChunkRequests(); call below:

AttachmentItem attachmentItem= new AttachmentItem
            { 
                Name = [Name],
                AttachmentType = AttachmentType.File,
Size = [Size]
            };

var session = graphClient.Users[USEREMAIL].Messages[MESSAGEID].Attachments.CreateUploadSession(attachmentItem).Request().PostAsync().Result;
            var stream = new MemoryStream(BYTEARRAY);
            var maxSizeChunk = DEFAULT_CHUNK_SIZE;
            var provider = new ChunkedUploadProvider(session, graphClient, stream, maxSizeChunk);
            var chunkRequests = provider.GetUploadChunkRequests();

(I am using the graphClient to send emails successfully, and have also used it to upload large files using the uploadSession method)

Mike
  • 629
  • 5
  • 18

1 Answers1

0

From Andrue Eastman on GitHub:

You are most likely getting the error because of using the ChunkedUploadPorvider instead of using the FileUploadTask to upload the attachment which is setting the Auth header to cause the error you are receiving.

To use the file upload task, follow the following steps

First create an upload session and handing it over to the task as illustrated.

// Create task
var maxSliceSize = 320 * 1024; // 320 KB - Change this to your chunk size. 4MB is the default.
LargeFileUploadTask<FileAttachment> largeFileUploadTask = new LargeFileUploadTask<FileAttachment>(uploadSession, stream, maxSliceSize);

Create an upload monitor (optional)

// Setup the progress monitoring
IProgress<long> progress = new Progress<long>(progress =>
{
    Console.WriteLine($"Uploaded {progress} bytes of {stream.Length} bytes");
});

The service only returns location URI which can be read off from the result object as follows.

UploadResult<FileAttachment> uploadResult = null;
try
{
    uploadResult = await largeFileUploadTask.UploadAsync(progress);
    if (uploadResult.UploadSucceeded)
    {
        Console.WriteLine(uploadResult.Location);//the location of the object
    }
}
catch (ServiceException e)
{
    Console.WriteLine(e.Message);
}
Mike
  • 629
  • 5
  • 18