如何修复 Microsoft graph api 抛出 邮箱处于非活动状态、软删除或在创建事件时在本地托管

问题描述 投票:0回答:1

我想创建约会日历事件。我已在天蓝色广告中添加了用户,并分配了委托权限 Calendars.ReadWrite。

但是在通过以下几行创建事件时会抛出错误:
var 响应 = 等待 graphClient.Me.Calendar.Events.Request().AddAsync(@event);

代码:MailboxNotEnabledForRESTAPI 消息:邮箱处于非活动状态、软删除或本地托管。

   public async Task<bool> CreateAppointmentAsync()
   {
       var spinId = Guid.NewGuid();
       StringBuilder apptBody = new StringBuilder();
       var httpClient = new System.Net.Http.HttpClient();
       System.Net.Http.HttpResponseMessage response;
       bool success = false;
       var username = _employee.Email;
       var employee = _employeeService.GetEmployeeByEmail(username);
       var employeeaccess = employee.EmployeeOfficeToken;
       string token = employeeaccess;
       //bool isTokenValid = IsTokenExpired(token);
       //if (isTokenValid)
       //{
       //    MessageBox.Show("Access token is still valid.");
       //}
       if (token == null || token.Length < 10)
       {
           var dialogResult = MessageBox.Show($"You do not has a security token for this function, Would you like to get one?", "Get Leads - No Security Token",
               MessageBoxButton.YesNo);
           if (dialogResult == MessageBoxResult.Yes)
           {
               _eventAggregator.GetEvent<FireToCbEvent>().Publish(new FireToCbInfo("Shell", null, "GetToken"));
           }
           return success;
       }
       // Start Spinner
       var spinner = new SetupSpinner(_eventAggregator, _container, _logMessage, spinId.ToString(), "Creating Appointment");

       try
       {
           var graphClient = new GraphServiceClient(
              new DelegateAuthenticationProvider((requestMessage) =>
              {
                  requestMessage
              .Headers
              .Authorization = new AuthenticationHeaderValue("bearer", token);

                  return System.Threading.Tasks.Task.CompletedTask;
              }));
           Event evt = new Event();

           // Because my variable is a reserved word in the api, I have to poke it into something different before using it
           var localLocation = Location;
           // Body
           const string newLine = " <br /> ";
           apptBody.Append("Email: " + SelectedEstimator.Email);
           apptBody.Append(newLine);
           apptBody.Append(EditCustomer.Notes);
           ItemBody body = new ItemBody();
           body.Content = apptBody.ToString();
           body.ContentType = Microsoft.Graph.BodyType.Html;

           var email = new Microsoft.Graph.EmailAddress
           {
               Address = SelectedEstimator.Email
           };
           email.Address = SelectedEstimator.Email;
           var recipient = new Recipient();
           recipient.EmailAddress = email;
           evt.Organizer = recipient;
           evt.IsOrganizer = true;
           evt.IsDraft = false;


           // Event General Information
           evt.Subject = Title;
           evt.Body = body;
           evt.Location = new Microsoft.Graph.Location
           {
               DisplayName = localLocation
           };

           // load up date and times
           // Start
           DateTimeTimeZone dtStart = new DateTimeTimeZone();
           dtStart.TimeZone = TimeZoneInfo.Local.Id;
           string dts = AppointmentDate.ToString("o");
           dtStart.DateTime = dts;
           // End
           DateTimeTimeZone dtEnd = new DateTimeTimeZone();
           dtEnd.TimeZone = TimeZoneInfo.Local.Id;
           string dte = AppointmentDate.AddHours(1).ToString("o");
           dtEnd.DateTime = dte;

           evt.Start = dtStart;
           evt.End = dtEnd;

           // Reminder off
           evt.IsReminderOn = false;

           // Create Event
           //var createdEvent = await graphClient.Me.Events.Request().AddAsync(evt);
           var createdEvent = await graphClient.Me.Calendar.Events.Request().AddAsync(evt);
           success = true;
           if (success)
               MessageBox.Show("Appointment Created!!");
           return success;
       }
       catch (Exception ex)
       {
           MessageBox.Show(ex.Message);
           return success;
       }
       finally
       {
           // Stop spinner
           Thread.Sleep(100);
           _eventAggregator.GetEvent<CloseControlEvent>().Publish(spinId.ToString());
       }
   }
c# wpf
1个回答
0
投票

很多讨论都说是License问题造成的,但我相信这不是唯一的原因。

我将 next.js 与

@microsoft/microsoft-graph-client
一起使用,它会抛出该错误。然后我打开 Azure 门户 > Microsoft Entra ID > 选择应用 > 选择快速启动 > 下载与您的技术匹配的示例项目(在我的例子中,我下载了 SPA 项目)。示例项目使用
@azure/msal-react
并且运行完美。我很确定我使用的是相同的配置(clientID、tenantID 等)。

我无法回答到底发生了什么,但我建议首先下载适合您技术的示例项目。

© www.soinside.com 2019 - 2024. All rights reserved.