Получить findMeetingTimes POST WebRequest - PullRequest
0 голосов
/ 27 октября 2018

Я пытаюсь позвонить POST на findMeetingTimes, используя Microsoft Graph в C #

https://graph.microsoft.com/v1.0/users/{userid}/findMeetingTimes

Чтобы получить данные профиля пользователя, у меня есть следующие данные:

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;
}

1 Ответ

0 голосов
/ 28 октября 2018

Лучшее предложение для вас - использовать клиентскую библиотеку Microsoft Graph напрямую, но не использовать HttpClient.После установки библиотеки, а затем просто используйте следующий код:

graphClient.Me.FindMeetingTimes().Request().PostAsync()

Это простой и эффективный способ сделать ваш код более читабельным.

Конечно,Вы можете написать свою собственную логику, имитируя следующий код:

 // 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");
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...