"code":"ErrorInvalidIdMalformed","message":"Id is malformed." while trying to list messages

Ata 0 Reputation points
2023-05-10T17:46:03.5966667+00:00

Hi,

I use Microsoft Graph SDK for Java v5.56.0 to access inbox of our users. Following code snippet was implemented to fetch the messages



User mailUser = client.me()
                .buildRequest()
                .get();


client.users(mailUser.id)
                .messages()
                .buildRequest(new QueryOption("$search", searchQuery),
                        expandOption, preferHeaderOption)
                .get();

Result of .get() on the last call is following.

https://graph.microsoft.com/v1.0/users/someUserId/messages?%24search=%22from%3Anoreply%40deutschebahn.com%20%28subject%3A%5C%22Booking%20confirmation%5C%22%20OR%20subject%3ABuchungsbest%C3%A4tigung%20OR%20subject%3A%5C%22Dein%20Zugticket%5C%22%20OR%20subject%3A%5C%22Rechnung%20zu%20Ihrer%20BahnCard%5C%22%29%20attachment%3Apdf%20received%3E%3D05%2F03%2F2023%22&%24expand=attachments%28%24select%3Did%2C%20name%2C%20size%29

Previously I tried with .me().messages(), as I got the same error, I decided to fetch the user separately.

Result of last call is 408Graph service exception. However, when I overrided the deserializeObject method of serializer to see the actual content of the response, I got

{"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#users('someUserId')/messages(attachments(id,name,size))","value":[{"error":{"code":"ErrorInvalidIdMalformed","message":"Id is malformed."}}

Therefore, I didn't get how Id could be malformed since I use the Id which was fetched from the user call? I appreciate if you could guide me to understand where I am missing.

Exchange Exchange Server Development
Microsoft Security Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 46,371 Reputation points
    2023-05-11T07:51:54.58+00:00

    Hi @Ata

    Do not call the me() method and the users("user id") method in the same context, these two methods require different authentication flows. If you want to list the message set of logged in users, then you can refer to my test code:

            MessageCollectionPage messagesPage = graphClient.me().messages()
                    .buildRequest()
                    .expand("attachments($select=id,name,size)")
                    .select("id,subject")
                    .top(10)
                    .get();
    
            while(messagesPage != null) {
                final List<Message> messages = messagesPage.getCurrentPage();
                final MessageCollectionRequestBuilder nextPage = messagesPage.getNextPage();
                if (nextPage == null) {
                    break;
                } else {
                    messagesPage = nextPage.buildRequest(
                            // Re-add the header to subsequent requests
                            new HeaderOption("Prefer", "outlook.body-content-type=\"text\"")
                    ).get();
                }
            }
    

    User's image

    Maven dependencies:

    User's image

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.