Reputation: 987
I am trying to make a POST
call to findMeetingTimes
using Microsoft Graph in C#
https://graph.microsoft.com/v1.0/users/{userid}/findMeetingTimes
To get user profile data, I have followings:
IUserProfile IUserAuthentication.GetProfileData(string accessToken)
{
MicrosoftUserProfile profile = new MicrosoftUserProfile();
try
{
string url = "https://graph.microsoft.com/v1.0/me";
Dictionary<string, string> headers = new Dictionary<string, string>();
headers.Add("Authorization", "Bearer " + accessToken);
WebRequests webReq = new WebRequests();
string response = webReq.GetRequest(url, headers);
profile = JsonConvert.DeserializeObject<MicrosoftUserProfile>(response);
}
catch (Exception)
{
throw;
}
return profile;
}
public string GetRequest(string url, IDictionary<string, string> headers)
{
string returned = "";
try
{
System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
webRequest.Method = "GET";
if (headers.Count > 0)
{
foreach (var item in headers)
{
webRequest.Headers.Add(item.Key, item.Value);
}
}
System.Net.WebResponse resp = webRequest.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
returned = sr.ReadToEnd().Trim();
}
catch (Exception)
{
throw;
}
return returned;
}
Upvotes: 0
Views: 229
Reputation: 1874
The best suggestion for you is to use the Microsoft Graph Client Library directly but not to use the HttpClient. After installing the library and then just use the following code:
graphClient.Me.FindMeetingTimes().Request().PostAsync()
This is a simple and efficient way to make your code more readable.
Of course, you can write your own logic by imitating the following code:
// 1. Create request message with the URL for the trending API.
string requestUrl = "https://graph.microsoft.com/V1.0/me/findMeetingTimes";
HttpRequestMessage hrm = new HttpRequestMessage(HttpMethod.Post, requestUrl);
// 2. Authenticate (add access token) our HttpRequestMessage
graphClient.AuthenticationProvider.AuthenticateRequestAsync(hrm).GetAwaiter().GetResult();
// 3. Send the request and get the response.
HttpResponseMessage response = graphClient.HttpProvider.PostAsync(hrm).Result;
// 4. Get the response.
var content = response.Content.ReadAsStringAsync().Result;
JObject responseBody = JObject.Parse(content);
// 4. Get the array of objects from the 'value' key.
JToken arrayOfObjects = responseBody.GetValue("value");
Upvotes: 1