Forum Discussion

Access_Jim's avatar
Access_Jim
Copper Contributor
Jul 28, 2024

Microsoft Graph API Error from VBA Program

I am attempting to send an email with a large (>3MB) attachment from my Access VBA program using the Microsoft Graph API.    I successfully created an upload session, but when I attempt to upload the attachment in chunks using this command:

 

PUT "https://graph.microsoft.com/v1.0/me/messages/AAMkADliNzgzNTk2LWJhMjgtNDM2My1iZTEwLTk3YzBjNDY1ZmQwZQBGAAAAAADu4ZwpgbXiRKTp5qfhjGayBwAMcsdba53uQYkVwk8WbRoaAAAAAAEPAAAMcsdba53uQYkVwk8WbRoaAAbrs5ILAAA=/attachments/Onboarding_Guide_v12.pdf/createUploadSession"

 

I receive an InvalidAuthenticationToken error.    The token that I used was good enough to create the upload session, and I thought that Microsoft kept track of that via the message ID but it seems to somehow be requesting it.

 

My VBA is below

 

Sub UploadFileInChunks(uploadUrl As String, filePath As String)
Dim fileData As String
fileData = ReadFile(filePath)

Dim fileSize As Long
fileSize = LenB(fileData)

Dim chunkSize As Long
'chunkSize = 320 * 1024
chunkSize = 30 * 1024

Dim startIndex As Long
startIndex = 0

Dim endIndex As Long
Dim numChunks As Long
numChunks = RoundUp(fileSize / chunkSize)

Dim xmlhttp As Object
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")

Dim chunk As String
Dim i As Long

For i = 1 To numChunks
endIndex = startIndex + chunkSize - 1
If endIndex > fileSize - 1 Then endIndex = fileSize - 1

chunk = MidB(fileData, startIndex + 1, endIndex - startIndex + 1)

Dim rangeHeader As String
rangeHeader = "bytes " & startIndex & "-" & endIndex & "/" & fileSize

'-----------------------------------------------------------
'uploadUrl provided as input is this:
'uploadUrl = "https://graph.microsoft.com/v1.0/me/messages/" & messageId & "/attachments/" & fileName & "/createUploadSession"
'-----------------------------------------------------------

xmlhttp.Open "PUT", uploadUrl, False
xmlhttp.setRequestHeader "Content-Range", rangeHeader
xmlhttp.setRequestHeader "Content-Type", "application/octet-stream"
xmlhttp.send chunk



If xmlhttp.Status <> 200 And xmlhttp.Status <> 201 And xmlhttp.Status <> 202 Then
MsgBox xmlhttp.Status
MsgBox "Error uploading chunk: " & xmlhttp.responseText, vbCritical
Exit Sub
End If

startIndex = endIndex + 1
Next i
End Sub

No RepliesBe the first to reply

Resources