问题
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);
}
})
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?
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
}
}
}
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
what is <id>
? It doesn't tell me what id is supposed to be?
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
Very confusing.
回答1:
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.
来源:https://stackoverflow.com/questions/34983786/how-can-i-create-a-calendar-event-on-outlook-with-microsoft-graph-api