Encounter 401 authentication via MS graph API, to accomplish send a mail / scheduling a meeting

本文介绍了解决在使用Microsoft Graph API发送邮件时遇到的401身份验证问题的方法。通过获取访问令牌,应用应用程序权限,并确保Office 365帐户拥有必要的许可证,实现了邮件发送功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Encounter 401 authentication issue, to accomplish send a mail / scheduling a meeting via MS graph api

  1. get access token via this link:https://dzone.com/articles/getting-access-token-for-microsoft-graph-using-oau

var authUrl = "https://login.microsoft.com";
				var appId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
				var appSecret = HttpUtility.UrlEncode("xxxxxxxxxxxxxxxxxxxxx");
				var tenantId = "xxxxxxxxxxxxxxxxxxx";

				var client = new HttpClient();
				var request = new HttpRequestMessage(HttpMethod.Post,
					$"{authUrl}/{tenantId}/oauth2/token");
				var bodyStr =
					$"grant_type=client_credentials&appId={appId}&resource={HttpUtility.UrlEncode("https://graph.microsoft.com")}&client_secret={appSecret}";
				var body = new StringContent(bodyStr);
				body.Headers.ContentType.MediaType = "application/x-www-form-urlencoded";
				request.Content = body;
				var getTokenResult = client.SendAsync(request).Result.Content.ReadAsStringAsync().Result;
				try
				{
					token = JObject.Parse(getTokenResult)["access_token"].ToString();
				}
				catch (Exception /*ex*/)
				{
					throw new Exception(getTokenResult);
				}

 appId, appSecret, tenantId, please refer to the link https://dzone.com/articles/getting-access-token-for-microsoft-graph-using-oau

  1. Call ms graph api

using (var client = new HttpClient())
				{
					//var mail = authContext.ConversationData.GetValue<SendAMail>("SendAMail");
					var jsonMail = JsonConvert.SerializeObject(mail);
					var content = new StringContent(jsonMail, Encoding.UTF8, "application/json");
					client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _token);
					client.DefaultRequestHeaders.Add("Accept", "application/json");

					using (
						var response =
						await
								client.PostAsync("https://graph.microsoft.com/v1.0/users/xxxx@dd.com/sendMail/", content))
					{
						string replyMsg;
						if (response.IsSuccessStatusCode)
							// replyMsg = "Done";
							replyMsg = NLS.Mctwo_TXT_MailDialog_Done;
						else
							//replyMsg = "I am sorry, I cannot connect to authorization server, email cannot be sent.";
							replyMsg = NLS.Mctwo_TXT_MailDialog_IASNC;

						await context.PostAsync(replyMsg);
					}
				}

Here, I apply the application permission, so that https://graph.microsoft.com/v1.0/users/xxxx@dd.com/sendMail/. the url contains users/o365 mail address. If you can apply delegate permission, it should be like this:https://graph.microsoft.com/v1.0/me/sendMail/

conclusion:

  • https://dzone.com/articles/getting-access-token-for-microsoft-graph-using-oau to grant ms graph permission
  • Your test office 365 account must own the license.or you are one owner of your application in Azure.However this application was created by o365 license account.
  • because I apply the application permission to execute MS graph api which requres Admin account to grant permission one time. 
### uVision5 中遇到的编译或运行时错误 在使用 Keil uVision5 进行嵌入式开发时,用户可能会遇到多种编译或运行时错误。这些问题通常与项目配置、代码语法、硬件设置或工具链兼容性有关。 #### 编译错误 1. **语法错误**:最常见的问题是源代码中的语法错误,例如缺少分号、括号不匹配或拼写错误。uVision5 会通过编译器输出窗口显示详细的错误信息,并指出错误发生的文件和行号[^1]。 2. **未定义的符号**:如果代码中引用了未声明的变量或函数,编译器会报错。确保所有外部符号都在正确的头文件中声明,并且包含路径配置正确[^1]。 3. **头文件缺失或路径错误**:如果编译器无法找到所需的头文件,通常是因为包含路径未正确设置。可以在 `Project > Options for Target > C/C++ > Include Paths` 中检查并添加必要的路径[^1]。 4. **库文件问题**:链接阶段可能出现“未解析的外部符号”错误,表明所需的库文件未正确链接。请确认使用的库是否已添加到项目中,并检查 `Project > Options for Target > Linker` 设置[^1]。 #### 运行时错误 1. **内存访问冲突**:如果程序尝试访问非法地址或未初始化的指针,可能导致系统崩溃或进入 Hard Fault 异常。建议使用调试器逐步执行代码,并查看调用栈以定位问题[^1]。 2. **堆栈溢出**:嵌入式系统中,尤其是使用递归或大型局部变量时,容易导致堆栈溢出。可以通过增加堆栈大小或优化代码结构来解决此问题[^1]。 3. **外设配置错误**:如果外设(如定时器、ADC 或 UART)未正确配置,可能导致程序运行异常。建议检查寄存器设置及相关的初始化代码[^1]。 4. **中断处理错误**:未正确配置或启用中断可能导致程序无法响应外部事件。确保中断服务例程(ISR)已正确实现,并在启动文件中注册相应的中断向量[^1]。 #### 调试建议 - 使用 uVision5 的调试功能(如断点、观察窗口和单步执行)来分析程序行为。 - 检查 `Build Output` 窗口中的警告和错误信息,这些信息通常能提供关键线索。 - 如果使用 STM32 等 ARM Cortex-M 系列 MCU,可以启用 ITM(Instrumentation Trace Macrocell)进行实时调试输出。 ### 示例代码:简单的 LED 闪烁程序(STM32F4xx) ```c #include "stm32f4xx.h" void delay(volatile uint32_t count) { while(count--) { __NOP(); } } int main(void) { // 启用 GPIOA 时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // 配置 PA5 为输出模式 GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStruct); while(1) { GPIO_SetBits(GPIOA, GPIO_Pin_5); // 点亮 LED delay(1000000); GPIO_ResetBits(GPIOA, GPIO_Pin_5); // 关闭 LED delay(1000000); } } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值