user6874415
user6874415

Reputation:

How to get the Microsoft graph result in my console application?

Currently, I am working in the console application(Visual Studio 2015) to get the group member detail (mail ID and name). For that, I searched lots to get the Microsoft Graphs output in my application. I found one document which says to register my app into the Azure portal. But I don`t wanna do this. I trying to achieve mine below idea.

I can able to get the desired output in the Graph Explorer. If I use the same API URL in the browser means I get the below error.

{
"error": {
"code": "InvalidAuthenticationToken",
"message": "Bearer access token is empty.",
"innerError": {
"request-id": "823434fb-a2cc-44a7-9bb7-1249c02f54a5",
"date": "2017-02-24T09:45:05"
    }
  }
}

For example, If I use the below URL in Graph explorer input box means I get my desired output. But If I use the same URL in the browser`s URL box I get the error.

https://graph.microsoft.com/v1.0/groups/922345b-4sc6-443c-ac69-02bh73536570/members?$top=900&$select=id,displayName

Is that Graph explorer URL works in the normal browser or not? If yes means How could I do it?

Update

I tried to get the output by using the below code

        string URL = "https://graph.microsoft.com/v1.0/groups/922345b-4sc6-443c-ac69-02bh73536570/members?$top=900&$select=id,displayName";
        System.Net.Http.HttpClient client = new    System.Net.Http.HttpClient();
        client.BaseAddress = new System.Uri(URL);
        byte[] cred = UTF8Encoding.UTF8.GetBytes("[email protected]:mypassword");
        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage messge = client.GetAsync(URL).Result;
        string result = messge.Content.ReadAsStringAsync().Result;

But I got the below output in string result

{
"error": {
"code": "InvalidAuthenticationToken",
"message": "CompactToken parsing failed with error code: -2147184105",
"innerError": {
  "request-id": "8gd6720-d558-4bbd-a0b8-fd098gw30f0b",
  "date": "2017-02-24T11:03:34"
  }
 }
}

Is there any different way or Did made any mistake?

Upvotes: 1

Views: 2061

Answers (1)

jpda
jpda

Reputation: 738

You must to register the application in AAD and get a bearer token - the graph API doesn't support basic authentication, which is what your code is doing. The document you linked will walk you through the process.

Upvotes: 2

Related Questions