none
Microsoft Graph API get email message attachments by message.id RRS feed

  • Question

  • Hi, 

    I'm trying to get an email message attachments via Microsoft Graph API with PowerShell.

    I'm already connected and collected messages with code

    $messages = Invoke-RestMethod -Method Get -Headers @{Authorization = "Bearer $accesstoken"; 'Content-Type' = 'application/json'} -Uri "https://graph.microsoft.com/v1.0/me/messages"
    foreach ($message in $messages.value) {
    if ($message.hasAttachments) {
    $encodedMessageId = [System.Web.HttpUtility]::UrlEncode($message.id) 
    $attachments = Invoke-RestMethod -Method Get -Headers @{Authorization = "Bearer $accesstoken"; 'Content-Type' = 'application/json'} -Uri "https://graph.microsoft.com/v1.0/me/messages/"+$encodedMessageId+"/attachments"
    }
    }

    but see an exception 

    Invoke-RestMethod : A positional parameter cannot be found that accepts argument '+A....AA%3d+/attachments'.
    At 111.ps1:14 char:18
    + ... tachments = Invoke-RestMethod -Method Get -Headers @{Authorization =  ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
        + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

    in examples I see what message.id looks like AAMkADA1M-zAAA= but mine looks like  

    +AAMkAGE3M2M3ZTFlLWEwOGMtNDRhZC1hZDhjLTliOGJjYmASDFG3NwASDFGHJKASDeTpIASDTbASDFxF1srASwBW6EGbH4lCR7VxqF2PrV17AAAAAAEMAABW6EGbH4lCR7VxqF2PrV17AAAAe1C6AAA%3d+ 

    what I'm doing wrong?

    Thursday, October 19, 2017 4:19 PM

All replies

  • Would you mind sharing the code you used to collect the messages? I'm having some trouble figuring that out. 
    Thursday, September 12, 2019 9:41 PM