microsoft-graph-api - Microsoft Graph API 获取事件开始返回 401 UnAuthorized
问题描述
我有一个使用 Microsoft Outlook rest api 来获取日历事件的应用程序。
该应用程序运行良好,但几天前我突然收到获取事件 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();
}
解决方案
能够通过将范围从 https://graph.microsoft.com/Calendars.ReadWrite更改为https://outlook.office.com/Calendars.ReadWrite来解决此问题。
随着我上面代码的这种变化,它现在开始正常工作了。
首先不确定为什么它更早地使用范围https://graph.microsoft.com/Calendars.ReadWrite
推荐阅读
- python-3.x - Python3:启动多进程时出现BrokenPipeError
- node.js - 尝试代理时出错:
:3000/api/v1/metadata,在 aws 中反应应用程序主机 - javascript - 单击按钮时增加和减少输入值
- reactjs - 下一个 JS 应用程序创建给出 package.json 错误
- docker - 为 go-pdk 插件配置企业代理
- python - Python 正则表达式以 abc 开头,任何路径都有 @\defg
- ios - 水平滚动视图中的 NavigationLink 和菜单
- outlook - 在我的 Web 应用程序上集成 MS Teams 视频通话
- c# - Razor View ForEach 在不同的列中
- java - 无法在eclipse中运行Spring Boot项目