Repository navigation
Closed
Labels
Description
Describe the bug
Attempt to upload a file to an email draft as an attachment fails. Each PUT request to upload a slice of the file gets a 401 error with the following payload.
{"error":{"code":"InvalidAudience","message":"The audience claim value is invalid '00000003-0000-0000-c000-000000000000/@'.","innerError":{"oAuthEventOperationId":"f58e4b30-148c-437e-849f-37ca5b7204a9","oAuthEventcV":"ep+jvnMUEywAMLY2BaKRxw.1.1","errorUrl":"https://aka.ms/autherrors#error-InvalidResource","requestId":"7f325e95-d7f8-4ef1-81d2-db9e1577f602","date":"2024-08-20T20:28:19"}}}Debug middleware reveals an Authorization header is being sent, which is likely the cause. From looking at the .NET implementation, the auth header is specifically omitted.
/// <param name="requestAdapter"><see cref="IRequestAdapter"/> to use for making upload requests. The client should not set Auth headers as upload urls do not need them.</param>Expected behavior
File should upload.
How to reproduce
func UploadAttachmentToMessage(graphClient *graph.GraphServiceClient, largeFile string) {
// <UploadAttachmentSnippet>
// Create message
message := models.NewMessage()
subject := "Large attachment"
message.SetSubject(&subject)
savedDraft, _ := graphClient.Me().Messages().Post(context.Background(), message, nil)
// Set up the attachment
byteStream, _ := os.Open(largeFile)
largeAttachment := models.NewAttachmentItem()
attachmentType := models.FILE_ATTACHMENTTYPE
largeAttachment.SetAttachmentType(&attachmentType)
fileName := filepath.Base(largeFile)
largeAttachment.SetName(&fileName)
fileInfo, _ := byteStream.Stat()
fileSize := fileInfo.Size()
largeAttachment.SetSize(&fileSize)
uploadSessionRequestBody := users.NewItemMessagesItemAttachmentsCreateUploadSessionPostRequestBody()
uploadSessionRequestBody.SetAttachmentItem(largeAttachment)
uploadSession, _ := graphClient.Me().
Messages().
ByMessageId(*savedDraft.GetId()).
Attachments().
CreateUploadSession().
Post(context.Background(), uploadSessionRequestBody, nil)
// Max slice size must be a multiple of 320 KiB
maxSliceSize := int64(320 * 1024)
fileUploadTask := fileuploader.NewLargeFileUploadTask[models.FileAttachmentable](
graphClient.RequestAdapter,
uploadSession,
byteStream,
maxSliceSize,
models.CreateFileAttachmentFromDiscriminatorValue,
nil)
// Create a callback that is invoked after each slice is uploaded
progress := func(progress int64, total int64) {
fmt.Printf("Uploaded %d of %d bytes", progress, total)
}
// Upload the file
uploadResult := fileUploadTask.Upload(progress)
if uploadResult.GetUploadSucceeded() {
fmt.Print("Upload complete")
} else {
fmt.Print("Upload failed.")
}
// </UploadAttachmentSnippet>
}SDK Version
1.47.0
Latest version known to work for scenario above?
No response
Known Workarounds
No response
Debug output
Click to expand log
```</details>
### Configuration
_No response_
### Other information
_No response_
Activity
andrueastman commentedon Aug 22, 2024
Thanks for raising this Jason Johnston (@jasonjoh)
I believe the
AzureIdentityAccessTokenProvidershould already prevent setting the Auth header for non-graph urls.https://github.com/microsoftgraph/msgraph-sdk-go/blob/cf048153191e603f83c8ae4f2bc60eb54daae03a/graph_service_client.go#L35
Any chance you can share how you are initializing the
GraphServiceClientinstance?On a different note, this check should probably be checking for the allowed hosts collection and not the scopes.
msgraph-sdk-go-core/authentication/azure_identity_access_token_provider.go
Line 34 in 202886d
jasonjoh commentedon Aug 22, 2024
Interesting. I was using the
AzureIdentityAuthenticationProviderfrom github.com/microsoft/kiota-authentication-azure-go, which I see doesn't set any valid hosts for the validator, resulting in all URLs being "valid".I changed to use the
AzureIdentityAuthenticationProviderfrom msgraph-sdk-go-core, and now I get this panic error:jasonjoh commentedon Aug 22, 2024
FYI I get the same panic if I switch back to using the auth provider from Kiota abstractions and pass in an array of valid hosts, like so:
andrueastman commentedon Aug 26, 2024
Jason Johnston (@jasonjoh) Any chance you can share a sample of the go.mod file as well?
Are you able to get the same error if the authentication lib and http package are the latest? I'm seeing a different issue on my end. Authored #321 for this.
jasonjoh commentedon Aug 26, 2024
As far as I can tell I have the latest versions.
andrueastman commentedon Aug 26, 2024
Just to confirm here, do you get the same error with the latest update after #321
jasonjoh commentedon Aug 26, 2024
That fixed it! Thanks for checking in.
andrueastman commentedon Aug 27, 2024
Thanks for confirming here. Closing this one for now..