You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Microsoft Graph API为Outlook日历事件设置颜色?

设置Outlook日历事件颜色(Microsoft Graph API)

创建Outlook日历事件时,无法直接通过创建接口的请求体设置颜色,需要分两步操作:

  1. 先调用创建事件接口生成事件,获取事件ID
  2. 通过PATCH接口更新事件的color字段来设置颜色

color字段支持的枚举值包括:

  • auto
  • lightBlue
  • lightGreen
  • lightOrange
  • lightGray
  • lightYellow
  • lightTeal
  • lightPink
  • lightBrown
  • lightRed

以下是修改后的完整代码:

RestClient restClient = new RestClient();
RestRequest restRequest = new RestRequest();

restRequest.AddHeader("Authorization", "Bearer " + refresh_tokenObj.outlookTokenViewModel.access_token);
restRequest.AddHeader("Content-Type", "application/json");
restRequest.AddParameter("application/json", JsonConvert.SerializeObject(calendarEvent), ParameterType.RequestBody);

restClient.Options.BaseUrl = new Uri("https://graph.microsoft.com/v1.0/me/calendar/events");
var response = restClient.Post(restRequest);

string eventId = string.Empty;
if (response.StatusCode == System.Net.HttpStatusCode.Created)
{
    dynamic data = JObject.Parse(response.Content);
    eventId = data.id;

    // 添加设置事件颜色的PATCH请求
    var patchRequest = new RestRequest($"https://graph.microsoft.com/v1.0/me/calendar/events/{eventId}", Method.Patch);
    patchRequest.AddHeader("Authorization", "Bearer " + refresh_tokenObj.outlookTokenViewModel.access_token);
    patchRequest.AddHeader("Content-Type", "application/json");
    
    // 示例设置为浅蓝色,可替换为支持的枚举值
    var colorBody = new { color = "lightBlue" };
    patchRequest.AddParameter("application/json", JsonConvert.SerializeObject(colorBody), ParameterType.RequestBody);
    
    var patchResponse = restClient.Execute(patchRequest);
    if (patchResponse.StatusCode == System.Net.HttpStatusCode.NoContent)
    {
        // 颜色设置成功
    }
}

注意事项

  • 应用必须拥有Calendars.ReadWrite权限,否则PATCH请求会返回权限错误
  • color字段必须使用官方指定的枚举值,无法自定义颜色

内容的提问来源于stack exchange,提问作者World News

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 07:03:35