2

I am working on a Microsoft Graph API app where i want to delete email messages from the inbox. I do that by first getting the emails, putting the id from each email in an array and for each id in that array making a delete request to remove it from the inbox.

When I try and run my code i get the error

Error: Server responded to *request link* with status code 403:
{
  "error": {
    "code": "ErrorAccessDenied",
    "message": "Access is denied. Check credentials and try again.",
    "innerError": {
      "request-id": "*request id*",
      "date": "2020-06-03T09:12:06"
    }
  }
}

When making the delete request I pass in the same Access Token that i use to make the get request for getting the email data.

Here is my code:

// This is only the code for the delete request, passing the Access Token works aswell as passing the IDList array. It loops trough all the id's and tries to make a request. But the request fails
removeEmail(IDList, AccessToken);

function removeEmail(idList, accessToken) {
    idList.forEach(ID => {
        var deleteEmails = request('DELETE', `https://graph.microsoft.com/v1.0/me/messages/${ID}`, {
            headers: {
                'Authorization': `Bearer ${accessToken}`
            }
        });

        deletedEmails = JSON.parse(deleteEmails.getBody('utf8'));

        console.log(deleteEmails);
    });
}

How can this be fixed so that it removes the messages from the inbox without any problems?

Thanks in advance!

1 Answers1

2

What permissions have you added to your Microsoft Graph API app?

Deleting an email requires Mail.ReadWrite - https://learn.microsoft.com/en-us/graph/api/message-delete?view=graph-rest-1.0&tabs=http

On the other hand, getting a list of messages works with the following permissions - Mail.ReadBasic, Mail.Read, Mail.ReadWrite - https://learn.microsoft.com/en-us/graph/api/user-list-messages?view=graph-rest-1.0&tabs=http#permissions

To be able to both read and delete email messages, you need Mail.ReadWrite added to your app.

shtrule
  • 658
  • 10
  • 23
  • I indeed have Mail.ReadWrite as permission. That's also why I am so confused as of why it is not working. – MrMilkshake Jun 03 '20 at 13:56
  • What about the note on the Delete mail reference page - "You may not be able to delete items in the recoverable items deletions folder (represented by the well-known folder name recoverableitemsdeletions). See Deleted item retention and Clean up deleted items for more information." (https://learn.microsoft.com/en-us/graph/api/message-delete?view=graph-rest-1.0&tabs=http) - Is it possible that the e-mail that you are trying to delete belongs to that folder? – shtrule Jun 03 '20 at 14:11
  • I will look into it, Thanks! – MrMilkshake Jun 03 '20 at 14:19