Microsoft Graph API获取事件开始返回401未经授权

时间:2018-07-30 09:31:55

标签: microsoft-graph outlook-restapi

我有一个使用Microsoft Outlook rest api来获取日历事件的应用程序。

遵循以下指示: https://docs.microsoft.com/en-us/previous-versions/office/office-365-api/api/version-2.0/use-outlook-rest-api

该应用程序运行正常,但是几天前突然出现了Get Events API的未授权错误(401)

查看响应头是一个例外 “ 2000003; reason =”受众声明值无效'https://graph.microsoft.com'。“; error_category =” invalid_resource“

下面是我的代码

HttpClient client = new HttpClient();
            var values = new Dictionary<string, string>
{
   { "client_id", "clientId" },
   { "scope", "https://graph.microsoft.com/Calendars.ReadWrite People.Read offline_access" },
   { "code", code },
   { "state", "12345" },
   { "redirect_uri", "http://localhost:3000/home" },
   { "grant_type", "authorization_code" },
   { "client_secret", "secret key" }
};

            var content = new FormUrlEncodedContent(values);

            var response = await client.PostAsync("https://login.microsoftonline.com/common/oauth2/v2.0/token", content);
            string token = string.Empty;
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var responseString = await response.Content.ReadAsStringAsync();
                var tokenResponse = JsonConvert.DeserializeObject<TokenResponse>(responseString);
                token = tokenResponse.access_token;
            }
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

            Events events = new Events();
            var responseEvents = await client.GetAsync("https://outlook.office.com/api/v2.0/me/events");
            if (responseEvents.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var responseString = await responseEvents.Content.ReadAsStringAsync();
                events = JsonConvert.DeserializeObject<Events>(responseString);
                ViewBag.Message = token;
            }
            else
            {
                var responseString = await responseEvents.Content.ReadAsStringAsync();
            }

1 个答案:

答案 0 :(得分:0)

能够通过更改范围来解决此问题 https://graph.microsoft.com/Calendars.ReadWritehttps://outlook.office.com/Calendars.ReadWrite

在我上面的代码中进行了此更改,现在开始正常工作。

首先不确定为什么它可以在范围https://graph.microsoft.com/Calendars.ReadWrite中使用较早