1

This is the code what I have tried.

Based on my limited knowledge it cannot display JSON response of Microsoft.Graph API, it just appears the message box with the content of Microsoft.Graph.User.

How can I parse the JsonResponse into the textbox.

Documentation of ListChat:

https://learn.microsoft.com/en-us/graph/api/chat-list?view=graph-rest-1.0

private async void button2_Click(object sender, EventArgs e)
{
    var scopes = new[] { "Chat.ReadBasic", "Chat.ReadWrite", "Chat.Read" };

    // Multi-tenant apps can use "common",
    // single-tenant apps must use the tenant ID from the Azure portal
    var tenantId = "5xxxx3-3xxa-4xxx1-9xx0c-exxxb0";
    // Value from app registration
    var clientId = "35xxxx-5xxx2-4xx9-8500-63xxxxaf";

    // using Azure.Identity;
    var options = new TokenCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    };

    var userName = "xx.xxxx@xxxi.com";
    var password = "Axxxxx@";

    // https://learn.microsoft.com/dotnet/api/azure.identity.usernamepasswordcredential
    var userNamePasswordCredential = new UsernamePasswordCredential(
                userName, password, tenantId, clientId, options);
    GraphServiceClient graphClient = new GraphServiceClient(userNamePasswordCredential, scopes);

    var request = await graphClient.Me.Request().GetAsync();
    String txt = request.ToString();
    MessageBox.Show(txt);
}

In Correct Response

user2250152
  • 14,658
  • 4
  • 33
  • 57
Johnny
  • 57
  • 7
  • It looks like you don't have a JSON response, but an object of type `Microsoft.Graph.User` which does not implement it's own `ToString()`, that's why you get the name of the type when using `ToString()`. – RedFox Oct 19 '22 at 09:29
  • Can I know how can I get the JSON response about List Chat on my code? I a bit confuse about the documentation @RedFox – Johnny Oct 19 '22 at 09:34
  • If your **really** need the data as JSON, use a Json serializer to convert it to a JSON string. If you just want to display some data, I would not recommend it. – RedFox Oct 19 '22 at 09:36

3 Answers3

1

You can create HttpRequestMessage, send this message using HttpProvider on GraphServiceClient to get a response and read the content. It should be json.

For current user

var httpMessage = graphClient.Me.Request().GetHttpRequestMessage();
var response = await graphClient.HttpProvider.SendAsync(httpMessage);
var jsonContent = await response.Content.ReadAsStringAsync();
MessageBox.Show(jsonContent);

For chats

var httpMessage = graphClient.Me.Chats.Request().GetHttpRequestMessage();
var response = await graphClient.HttpProvider.SendAsync(httpMessage);
var jsonContent = await response.Content.ReadAsStringAsync();
MessageBox.Show(jsonContent);
user2250152
  • 14,658
  • 4
  • 33
  • 57
  • if I wnt to show the response of create chat by following this documentation https://learn.microsoft.com/en-us/graph/api/chat-post?view=graph-rest-1.0&tabs=http is it be able to show it with http request message method – Johnny Oct 20 '22 at 03:06
0

At

  var request = await graphClient.Me.Request().GetAsync();

you do not have JSON which you have to parse, but an object of type Microsoft.Graph.User. Since this object does not implement it's own .ToString() it gives you the result of the base implementation which is it's object type name.

If you really need the object as JSON, you can use a JSONserializer like Newstonsoft provides and use it to serialize your data to a JSON string.

If you want to display data of the user you have to access it's properties and display their values.

RedFox
  • 457
  • 3
  • 15
-1

You can try to deserialize it with Newtonsoft.Json

https://www.nuget.org/packages/Newtonsoft.Json/

To do that you need a class with the same properties as the .json file. So something like:

public class Response
{
    public List<Value> Values {get;set;}
}

public class Value
{
    public string Id {get;set;}
    public string Topic {get;set;
    .....
    public ChatViewPoint ChatViewPoint {get;set;}
}
public class ChatViewPoint
{
    public bool IsHidden {get;set;}
    ....
}

After you created the classes you can try to deserialize the string to the given datamodel.

Hope it works

zTim
  • 78
  • 5