Microsoft graph api Microsoft Graph API Get Events started返回未经授权的401
我有一个应用程序,它使用Microsoft Outlook rest api获取日历事件 按照以下说明进行操作: 应用程序运行正常,但几天前突然出现Get Events API未经授权的错误(401) 查看响应头,这是一个例外 “2000003;reason=”受众声明值无效“”。“error\u category=”invalid\u resource” 下面是我的代码Microsoft graph api Microsoft Graph API Get Events started返回未经授权的401,microsoft-graph-api,outlook-restapi,Microsoft Graph Api,Outlook Restapi,我有一个应用程序,它使用Microsoft Outlook rest api获取日历事件 按照以下说明进行操作: 应用程序运行正常,但几天前突然出现Get Events API未经授权的错误(401) 查看响应头,这是一个例外 “2000003;reason=”受众声明值无效“”。“error\u category=”invalid\u resource” 下面是我的代码 HttpClient client = new HttpClient(); var values
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();
}
HttpClient=newhttpclient();
var值=新字典
{
{“客户端id”、“客户端id”},
{“范围”https://graph.microsoft.com/Calendars.ReadWrite 人。读取脱机访问“},
{“代码”,代码},
{“州”、“12345”},
{“重定向uri”http://localhost:3000/home" },
{“授权类型”、“授权代码”},
{“客户机密钥”,“密钥”}
};
var内容=新的FormUrlEncodedContent(值);
var response=wait client.PostAsync(“https://login.microsoftonline.com/common/oauth2/v2.0/token“,内容);
string-token=string.Empty;
if(response.StatusCode==System.Net.HttpStatusCode.OK)
{
var responseString=await response.Content.ReadAsStringAsync();
var tokenResponse=JsonConvert.DeserializeObject(responseString);
token=tokenResponse.access\u token;
}
client.DefaultRequestHeaders.Add(“Accept”、“application/json”);
client.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(“承载者”,令牌);
事件=新事件();
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(responseString);
Message=token;
}
其他的
{
var responseString=await responseEvents.Content.ReadAsStringAsync();
}
通过将作用域从
对
在我上面的代码中做了这样的更改,现在它开始正常工作了
首先不确定它为什么在早期使用scope您是否尝试调用(您正在请求的sope)而不是Outlook API端点?你的意思是,我也尝试了这个端点,它说Bad requesthmm..你能使用Graph Explorer访问事件吗?另外,包括一个你得到的令牌示例可能会很有帮助。