question

Laurens-4345 avatar image
0 Votes"
Laurens-4345 asked Laurens-4345 commented

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

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-mail
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered

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.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Laurens-4345 avatar image
0 Votes"
Laurens-4345 answered Laurens-4345 commented

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.

· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yes me only works for the current user which is how I interpreted your original "i want to get my messages" comment.

In the OP you said it was failing when using {id}/messages but in the above code you aren't using that URL at all. Is the retrieval of the user list failing? If not then please post where you're using the endpoint for getting the user's messages.

0 Votes 0 ·

In the OP i used {<!-- -->{ id }} to replace my user-id. .

if i check the below link
https://docs.microsoft.com/en-us/graph/api/user-list-messages?view=graph-rest-1.0&tabs=http

There are two ways of getting my messages.
- GET /me/messages (signed in user i am not using this)
- GET /users/{id | userPrincipalName}/messages (This is what I'm aiming for)

I used the snippet below to get my user ID:
```
$userList = Invoke-WebRequest -Method Get -UseBasicParsing -Headers $headerParams -Uri $url | ConvertFrom-Json
ForEach ($user In $userList.Value) {
$user
}
```

And i used that same ID with the URL below:

which gave me the error: Invoke-WebRequest : The remote server returned an error: (404) Not Found.



0 Votes 0 ·

The first IWR call isn't getting your user ID. It is getting all the users which you then enumerate through. Does that call return back the users correctly?

Inside the foreach loop, to get the messages for the given user, you'd need to call IWR with a properly formatted URL to get the messages. Something like

$url = "https://graph.microsoft.com/v1.0/users/${$user.userPrincipalName}/messages"
Invoke-WebRequest -Method Get -Headers $headerParams -Uri $url | ConvertFrom-Json
0 Votes 0 ·
Show more comments