ODataError: Exception of type 'Microsoft.Graph.Models.ODataErrors.ODataError' was thrown.

Shivakumar Mucharla 20 Reputation points
2023-07-28T15:00:59.8033333+00:00

I am trying to read Inbox of User mail I am not able to read
I am using DotNet FrameWork 4.8

Exception
EUser's image

namespaces

using Azure.Identity;

using Microsoft.Exchange.WebServices.Data;

using Microsoft.Graph;

using Microsoft.Graph.Models;

using Microsoft.Graph.Models.ODataErrors;

using Microsoft.Identity.Client;

using System.IO;

using Newtonsoft.Json;

using System;

using System.Collections.Generic;

using System.Configuration;

using System.Linq;

using System.Threading.Tasks;

using BodyType = Microsoft.Graph.Models.BodyType;

using EmailAddress = Microsoft.Graph.Models.EmailAddress;

App.Config ( giving sample values )

<appSettings>
		<add key="tenantId" value="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" />
		<add key="appId" value="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" />
		<add key="clientSecret" value="ccccccccccccccccccccccccccccc" />
		<add key="NetworkUserName" value="******@abc.com" />
	</appSettings>

My code

 public static string ReadingInboxMailsUsingGraphAPI()
        {
            try
            {
                string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
                string tenantId = ConfigurationManager.AppSettings["tenantId"];
                string appId = ConfigurationManager.AppSettings["appId"];
                string clientId = ConfigurationManager.AppSettings["clientSecret"];
                var graphServiceClient = new GraphServiceClient(new ClientSecretCredential(tenantId, appId, clientId));
                string user = ConfigurationManager.AppSettings["NetworkUserName"];
                var unreadInboxMessages = graphServiceClient.Users[user].MailFolders["inbox"].Messages.GetAsync(
                    requestConfiguration => {
                        //requestConfiguration.QueryParameters.Select = new string[] { "id", "createdDateTime" };
                        requestConfiguration.QueryParameters.Top = 50;
                      
                    }).Result;
                return "Read successfully.";

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,038 questions
{count} votes

Accepted answer
  1. CarlZhao-MSFT 45,186 Reputation points
    2023-07-31T08:38:44.6333333+00:00

    Hi @Shivakumar Mucharla

    Your error seems to be thrown, only from the type of exception you shared I can't find the cause of the problem, I suggest you catch the error message to know the problem details.

    If you're trying to list messages in a user's mailbox in an application-only context, then make sure to:

    1. The user is a work account (not a personal or guest account) and is in the current tenant.
    2. Make sure the target user has a Microsoft 365 license.
    3. Make sure the application has one of the Mail.ReadBasic.All, Mail.Read, or Mail.ReadWrite application permissions.
    4. Make sure your administrator has not configured application access policies to restrict application access to mailboxes.

    I did a quick test locally with the graph SDK and it works fine:

    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.


1 additional answer

Sort by: Most helpful
  1. Hagen 30 Reputation points
    2023-08-01T09:57:03.89+00:00

    As mentioned by CarlZhao-MSFT, you have to catch the error message to know the problem details. The return code might be an unauthorized (401), forbidden (403) or bad request (400).

    Try the following, which works for me:

    var scopes = new[] {"https://graph.microsoft.com/.default" };
    var tenantId = ConfigurationManager.AppSettings["tenantId"];
    var clientId = ConfigurationManager.AppSettings["appId"]; 
    var clientSecret = ConfigurationManager.AppSettings["clientSecret"];
    
    var options = new TokenCredentialOptions
    {
    	AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    };
    
    var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
    
    _gsclient = new GraphServiceClient(clientSecretCredential, scopes);
    ...
    
    0 comments No comments

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.