Graph | Invoke-WebRequest : The remote server returned an error: (404) Not Found.

Laurens 1 Reputation point
2021-05-25T13:21:38.943+00:00

Hi,

I am trying to use powershell (Not SDK) to get a list of my e-mail messages and later on my attachments.
I am using a similar script as is shown here: https://scripting.up-in-the.cloud/powershell/graph-scripting.html#script which is working great for authenticating and getting my token and user data via: 'https://graph.microsoft.com/v1.0/users/' using Application permissions in Azure AD.

When i try to get my message list --and for this i am using the graph URL i tested with explore.graph- using: https://graph.microsoft.com/v1.0/users/{{ iD }}/messages
i constantly get a 404 resources not found error as shown in the title.
I would suspect the results to be similar to the explore.graph

Please advice on how to get a list of messages using powershell (if this is still the way to go).

Regards

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,768 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 49,056 Reputation points
    2021-05-25T13:38:42.033+00:00

    404 means the URL is wrong. You didn't post the full PS code so I can only guess what is wrong.

    1. For your personal messages you can use the simplified URL: https://graph.microsoft.com/v1.0/me/messages.
    2. The HTTP method needs to be GET. In the sample script they were making a change but you're fetching data so GET is the correct verb. Failure to do this will result in a 404.
    3. The {iD} is not a valid PS variable and therefore it ends up being blank producing a bad URL.

    I would recommend that you put the templated URL into a temp variable and verify it is correct before using it with IWR.

    0 comments No comments

  2. Laurens 1 Reputation point
    2021-05-25T14:02:46.793+00:00

    Thank you for the rapid response.
    My understanding of the personal messages is that '/me/messages' is using the signed in user auth option and i am trying to make a application with a token auth.
    Therefor the '/me/messages' is resulting in a 403 no auth error. Which is understandable.

    Here is the PS code i am using. (Client id , tenant and secret vars removed).

    $clientID = ""
    $tenantdomain = ""
    $clientSecret = ""
    
    $loginURL     = "https://login.microsoft.com"
    $resource     = "https://graph.microsoft.com"
    $body         = @{grant_type="client_credentials";resource=$resource;client_id=$ClientID;client_secret=$ClientSecret}
    $oauth        = Invoke-RestMethod -Method Post -Uri $loginURL/$tenantdomain/oauth2/token?api-version=1.0 -Body $body
    $headerParams = @{'Authorization'="$($oauth.token_type) $($oauth.access_token)"}
    
    #_____________________________________________________________________________________________
    
    $url = 'https://graph.microsoft.com/v1.0/users/'
    
    ## This lists my user information
    $userList = Invoke-WebRequest -Method Get -UseBasicParsing -Headers $headerParams -Uri $url | ConvertFrom-Json
    
    ForEach ($user In $userList.Value) {
        $user
    }
    
    ##  i add this to test the messages. 
    userList = Invoke-WebRequest -Method Get -UseBasicParsing -Headers $headerParams -Uri $url | ConvertFrom-Json
    

    The code above gives me my user information and ID.
    When i alter the URL to 'https://graph.microsoft.com/v1.0/users/MYUSERID/messages' i get the 404.

    When i try the same url method using the https://developer.microsoft.com/en-us/graph/graph-explorer my messages are listed.