want to fetch the self(ME) conversation using client secret can anyone help me to get it ?

Raj Gandhi 0 Reputation points
2024-01-05T11:04:53.6166667+00:00
Hello,

want to fetch the self(ME) conversation using client secret can anyone help me to get it ?

I am trying to get the self conversation in teams using Microsoft Graph API using Application credential and not delegate method but I always get the error of Microsoft.Graph.Models.ODataErrors.ODataError

This exception was originally thrown at this call stack:

    Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.ThrowIfFailedResponse(System.Net.Http.HttpResponseMessage, System.Collections.Generic.Dictionary<string, Microsoft.Kiota.Abstractions.Serialization.ParsableFactory<Microsoft.Kiota.Abstractions.Serialization.IParsable>>, System.Diagnostics.Activity)

    Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync<ModelType>(Microsoft.Kiota.Abstractions.RequestInformation, Microsoft.Kiota.Abstractions.Serialization.ParsableFactory<ModelType>, System.Collections.Generic.Dictionary<string, Microsoft.Kiota.Abstractions.Serialization.ParsableFactory<Microsoft.Kiota.Abstractions.Serialization.IParsable>>, System.Threading.CancellationToken)

    Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.SendAsync<ModelType>(Microsoft.Kiota.Abstractions.RequestInformation, Microsoft.Kiota.Abstractions.Serialization.ParsableFactory<ModelType>, System.Collections.Generic.Dictionary<string, Microsoft.Kiota.Abstractions.Serialization.ParsableFactory<Microsoft.Kiota.Abstractions.Serialization.IParsable>>, System.Threading.CancellationToken)

    Microsoft.Graph.Users.Item.Chats.Item.Messages.MessagesRequestBuilder.GetAsync(System.Action<Microsoft.Graph.Users.Item.Chats.Item.Messages.MessagesRequestBuilder.MessagesRequestBuilderGetRequestConfiguration>, System.Threading.CancellationToken)

    GraphTutorial.Demo.GetSelfConversation() 

and want to fetch the ME conversation using client secret can anyone help me to get it ?

The code I'm using is :

var scopes = new[] { "https://graph.microsoft.com/.default" };

var options = new ClientSecretCredentialOptions

{

    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud

};

bool IsClientCredential = true;

if (IsClientCredential)

{

    _clientSecretCredential = new ClientSecretCredential(TenantId, ClientId, ClientSecret, options);

    _graphServiceClient = new GraphServiceClient(_clientSecretCredential, scopes);

}

var selfConversationMessages = _graphServiceClient.Users["user-id"].Chats[_selfConversationId].Messages.GetAsync().GetAwaiter().GetResult();

foreach (var message in selfConversationMessages.Value)

{

    Console.WriteLine(message.Body.Content);

}
Microsoft Teams
Microsoft Teams
A Microsoft customizable chat-based workspace.
9,334 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,998 questions
Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
2,946 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 38,436 Reputation points
    2024-01-08T06:52:49.86+00:00

    Hi @Raj Gandhi

    I can't find the problem from the exception type you shared. Please use try-catch to catch specific error messages.

    Refer to the following case:

    using Microsoft.Graph.Models.ODataErrors;
    
    try 
    {
        var messages = await graphClient.Users["{user id}"].Chats["{chat id}"].Messages.GetAsync();
    
        foreach (var message in messages.Value)
        {
    
            Console.WriteLine(message.Body.Content);
    
        }
    }
    
    catch (ODataError odataError)
    {
    
        Console.WriteLine(odataError.Error.Code);
        Console.WriteLine(odataError.Error.Message);
    
    }
    

    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.