Graph API not working - ( Unhandled exception. Microsoft.Kiota.Abstractions.ApiException: The server returned an unexpected status code and the error registered for this code failed to deserialize: 500 )

Kamal Kumar 0 Reputation points
2025-06-05T12:05:13.2066667+00:00

Using correct credentials as clientid , tenantid , clientsecret key
some times unable to read mail data and getting exception as below ------------------>

Unhandled exception. Microsoft.Kiota.Abstractions.ApiException: The server returned an unexpected status code and the error registered for this code failed to deserialize: 500

at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.ThrowIfFailedResponseAsync(HttpResponseMessage response, Dictionary`2 errorMapping, Activity activityForAttributes, CancellationToken cancellationToken)

at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsyncModelType

at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsyncModelType

at Microsoft.Graph.Users.Item.MailFolders.Item.Messages.MessagesRequestBuilder.GetAsync(Action`1 requestConfiguration, CancellationToken cancellationToken)

at Program.Main(String[] args) in C:\Users\avis\Desktop\email\GraphMailReader\Program.cs:line 22

at Program.<Main>(String[] args)

BELOW CODE USED ------------------------------------------------->

 var scopes = new[] { "https://graph.microsoft.com/.default" };
            var credential = new ClientSecretCredential(emailConfig.OutlookTenantId, emailConfig.OutlookClientId, emailConfig.OutlookClientSecret);
            var graphClient = new GraphServiceClient(credential, scopes);
         try
        {
            var messages = await graphClient.Users[emailConfig.incomingmailid].MailFolders["Inbox"].Messages
                .GetAsync(req =>
                {
                    req.QueryParameters.Top = 1;
                    req.QueryParameters.Select = new[] { "subject", "from", "receivedDateTime", "body", "bodyPreview", "internetMessageId", "internetMessageHeaders" };
                    req.QueryParameters.Orderby = new[] { "receivedDateTime desc" };
                });

Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

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.