【问题标题】:How do I properly represent an Event object for Microsoft Graph?如何正确表示 Microsoft Graph 的 Event 对象?
【发布时间】:2016-07-08 04:47:10
【问题描述】:

我目前正在尝试使用带有 ASP .NET MVC 的 Microsoft Graph API 在用户 Outlook 日历上创建事件。不幸的是,创建事件的文档不包括示例。我已经找到并尝试修改发送电子邮件的示例(此处:https://github.com/microsoftgraph/aspnet-connect-rest-sample)。我能够将一个完全空白的初始事件发送到我的日历。当尝试发送事件对象本身时,我遇到了状态响应 BAD REQUEST。如果有人可以提供帮助,将不胜感激。

【问题讨论】:

    标签: asp.net office365 microsoft-graph-api


    【解决方案1】:

    有关如何构造 Event 对象的参考,您可以查看官方 Microsoft Graph SDK 是如何构造该对象的:请参阅 GitHub 上的最新源 here

    有关在不使用 SDK 的情况下进行此 REST 调用的示例,您可以参考 UserSnippets#CreateEventAsync() - 摘录粘贴在下面。虽然下面的示例没有使用本机对象进行序列化,但它希望能传达其工作原理的本质。

    HttpClient client = new HttpClient();
    var token = await AuthenticationHelper.GetTokenHelperAsync();
    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
    
    // Endpoint for the current user's events
    Uri eventsEndpoint = new Uri(serviceEndpoint + "me/events");
    
    // Build contents of post body and convert to StringContent object.
    // Using line breaks for readability.
    
    // Specifying the round-trip format specifier ("o") to the DateTimeOffset.ToString() method
    // so that the datetime string can be converted into an Edm.DateTimeOffset object:
    // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Roundtrip
    
    string postBody = "{'Subject':'Weekly Sync'," + "'Location':{'DisplayName':'Water Cooler'}," + "'Attendees':[{'Type':'Required','EmailAddress': {'Address':'mara@fabrikam.com'} }]," + "'Start': {'DateTime': '" + new DateTime(2014, 12, 1, 9, 30, 0).ToString("o") + "', 'TimeZone':'UTC'}," + "'End': {'DateTime': '" + new DateTime(2014, 12, 1, 10, 0, 0).ToString("o") + "', 'TimeZone':'UTC'}," + "'Body':{'Content': 'Status updates, blocking issues, and next steps.', 'ContentType':'Text'}}";
    
    var createBody = new StringContent(postBody, System.Text.Encoding.UTF8, "application/json");
    
    HttpResponseMessage response = await client.PostAsync(eventsEndpoint, createBody);
    
    if (response.IsSuccessStatusCode) {
     string responseContent = await response.Content.ReadAsStringAsync();
     jResult = JObject.Parse(responseContent);
     createdEventId = (string) jResult["id"];
     Debug.WriteLine("Created event: " + createdEventId);
    } else {
     // some appropriate error handling here
    }
    

    有关传输的 JSON 的示例:

    {
        "subject": "Weekly Sync",
        "location": {
            "displayName": "Water Cooler"
        },
        "attendees": [
            {
                "type": "Required",
                "emailAddress": {
                    "address": "mara@fabrikam.com"
                }
            }
        ],
        "start": {
            "dateTime": "2016-02-02T17:45:00.0000000",
            "timeZone": "UTC"
        },
        "end": {
            "dateTime": "2016-02-02T18:00:00.0000000",
            "timeZone": "UTC"
        },
        "body": {
            "content": "Status updates, blocking issues,and nextsteps.",
            "contentType": "Text"
        }
    }
    

    其他帮助: 有一个有用的 Graph Explorer Sandbox 可供您在浏览器中测试请求

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-07
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      相关资源
      最近更新 更多