如何使用Microsoft Graph API在Outlook上创建日历事件? [英] How can I create a calendar event on Outlook with Microsoft Graph API?

查看:217
本文介绍了如何使用Microsoft Graph API在Outlook上创建日历事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与Office365集成的应用程序,我试图使用Microsoft Graph API在Outlook日历上创建日历事件.这是我到目前为止的内容:

I have an app that integrates with Office365 and I am attempting to create a calendar event on an Outlook calendar using the Microsoft Graph API. Here is what I have so far:

        request.post({
            url:'https://graph.microsoft.com/v1.0/me/events',
            form: {
                "Id": null,
                "Subject": "Discuss the Calendar REST API",
                "Body": {
                    "ContentType": "Text",
                    "Content": "This is some content."
                },
                "Start": {
                    "DateTime": "2016-01-24T18:00:00",
                    "TimeZone": "Pacific Standard Time"
                },
                "End": {
                    "DateTime": "2016-01-25T19:00:00",
                    "TimeZone": "Pacific Standard Time"
                },
                "ShowAs": "Free",
                "IsReminderOn":false
            },
            headers: {
                "Authorization": "Bearer " + access_token,
                "Content-Type": "application/json"
            }
        }, function(err, httpResponse, body) {
            if (err) {
                console.log('addMicrosoftAccessToken() ERROR = ' + err);
                callback(err, false);
            } else {
                console.log('httpResponse = ' + JSON.stringify(httpResponse));
                callback(null, true);
            }
        })

问题在于该事件未保存在用户的Outlook日历中.另外,我在日志中没有出现错误.我怀疑我没有在请求中发送适当的表格数据.有什么想法吗?

The problem is that the event isn't saved on the users Outlook calendar. Also, I'm not getting an error in the log. I suspect that I'm not sending the proper form data in the request. Any ideas?

更新:这是我在日志中得到的httpResponse:

UPDATE: Here is the httpResponse I'm getting in the log:

{
    "statusCode": 500,
    "body": "{\r\n  \"error\": {\r\n    \"code\": \"UnknownError\",\r\n    \"message\": \"\",\r\n    \"innerError\": {\r\n      \"request-id\": \"8ebe2efc-649c-4d8d-bee1-be2457cc3a45\",\r\n      \"date\": \"2016-01-25T19:05:27\"\r\n    }\r\n  }\r\n}",
    "headers": {
        "cache-control": "private",
        "transfer-encoding": "chunked",
        "content-type": "application/json",
        "server": "Microsoft-IIS/8.5",
        "request-id": "8ebe2efc-649c-4d8d-bee1-be2457cc3a45",
        "client-request-id": "8ebe2efc-649c-4d8d-bee1-be2457cc3a45",
        "x-ms-ags-diagnostic": "{\"ServerInfo\":{\"DataCenter\":\"East US\",\"Slice\":\"SliceB\",\"ScaleUnit\":\"000\",\"Host\":\"AGSFE_IN_4\",\"ADSiteName\":\"EST\"}}",
        "outboundduration": "707.5019",
        "duration": "713.2419",
        "x-powered-by": "ASP.NET",
        "date": "Mon, 25 Jan 2016 19:05:27 GMT",
        "connection": "close"
    },
    "request": {
        "uri": {
            "protocol": "https:",
            "slashes": true,
            "auth": null,
            "host": "graph.microsoft.com",
            "port": 443,
            "hostname": "graph.microsoft.com",
            "hash": null,
            "search": null,
            "query": null,
            "pathname": "/v1.0/me/events",
            "path": "/v1.0/me/events",
            "href": "https://graph.microsoft.com/v1.0/me/events"
        },
        "method": "POST",
        "headers": {
            "Authorization": "Bearer blah blah",
            "Content-Type": "application/x-www-form-urlencoded",
            "Accept": "application/json",
            "content-length": 643
        }
    }
}

更新2: 该链接的标题为创建事件",并且似乎在请求和响应部分中都列出了响应,这使得它特别令人困惑: http://graph.microsoft.io/docs/api-reference /v1.0/api/event_post_instances 另外,在上面列出的链接中

UPDATE 2: This link is titled "Create Event", and appears to have the response listed in both the request and response sections, making it particularly confusing: http://graph.microsoft.io/docs/api-reference/v1.0/api/event_post_instances Also, in the above link where it lists

POST https://graph.microsoft.com/v1.0/me/events/<id>/instances

什么是<id>?它没有告诉我ID应该是什么?

what is <id>? It doesn't tell me what id is supposed to be?

更新3:此链接的标题也为创建事件",但它具有不同的POST URL:

UPDATE 3: This link is also titled "Create Event", yet it has a different POST URL:

http://graph.microsoft.io/docs/api-reference/v1.0/api/user_post_events

非常令人困惑.

推荐答案

我/事件"请求失败的原因是您使用Azure Active Directory(AAD)授权流访问您的个人Microsoft帐户(实时ID)日历. AAD允许创建映射到Microsoft帐户的用户,以便在请求AAD令牌时使用您的Microsoft帐户凭据登录.这样,您的凭据可用于访问企业(工作或学校)和消费者(个人)服务.共享凭据时,有2个不同的用户帐户.当访问诸如OneDrive for Business或SharePoint之类的商业服务时,您需要在工作或学校组织的上下文中使用AAD用户帐户登录.当访问诸如Hotmail或OneDrive之类的消费者服务时,您需要使用Microsoft帐户(实时ID)进行登录.您的请求正在尝试在组织的上下文中访问Outlook帐户,该帐户不存在,因为与您的Microsoft帐户相关联的个人Outlook帐户已经提供了您的电子邮件地址.请使用融合授权流程( http://graph.microsoft.io/zh-我们/docs/authorization/converged_auth )以使用您的个人Microsoft帐户登录,您将可以访问您的个人电子邮件和日历.另外,您可以继续使用AAD授权流程访问未映射到Microsoft帐户的AAD用户的工作或学校日历.

The reason why your me/events request fails is that you used the Azure Active Directory (AAD) authorization flow for accessing your personal Microsoft Account (Live Id) calendar. AAD allows creating users mapped to Microsoft Accounts such that when requesting an AAD token you're signing in with your Microsoft Account credentials. That way your credentials can be used to access both business (work or school) and consumer (personal) services. While the credentials are shared there are 2 distinct user accounts. When accessing business services like OneDrive for Business or SharePoint you need to sign in with the AAD user account in the context of your work or school organization. When accessing consumer services like Hotmail or OneDrive you need to sing in with the Microsoft Account (Live Id). You request is attempting to access an Outlook account in the context of your organization, which doesn't exist as your email address is already served by a personal Outlook account associated with your Microsoft Account. Please use the converged authorization flow (http://graph.microsoft.io/en-us/docs/authorization/converged_auth) to sign in with your personal Microsoft Account and you'll be able to access your personal email and calendar. Alternatively you can keep using the AAD authorization flow to access work or school calendars for AAD users not mapped to Microsoft Accounts.

这篇关于如何使用Microsoft Graph API在Outlook上创建日历事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆