Search code examples
pythondjangopython-requestsmicrosoft-graph-apimicrosoft-graph-calendar

Post data to Outlook calendar using Django, Requests, and Microsoft Graph


I have a function where I am trying to make a post request to my outlook calendar using Microsoft’s Graph API.

def create_calendar_event(token, payload, **kwargs):
    """
    Creates a new calendar event
    :param payload: json str
    :param token: str
    :return: dict
    """
    graph_client = OAuth2Session(token=token)
    url_endpoint = "https://graph.microsoft.com/v1.0/me/events"
    events = graph_client.post(url=url_endpoint, json=payload)

    return events.json()

In my view I am using a form to get the data from the user so I can in return post the user data to my Outlook calendar. The form looks something like this:

class CalendarFormView(TemplateView):
    template_name = "event-form.html"

    def get(self, request, **kwargs):
        form = CalendarEventForm()
        return render(request, self.template_name, {"form": form})

    def post(self, request):
        form = CalendarEventForm(request.POST)
        token = client.get_token(request)  # does get the token correctly

        if form.is_valid():
            subject = form.cleaned_data["subject"]
            content = form.cleaned_data["body_content"]
            start = form.cleaned_data["start"]
            end = form.cleaned_data["end"]
            location = form.cleaned_data["location"]
            is_all_day = form.cleaned_data["is_all_day"]

            payload = {
                "subject": subject,
                "body": {"contentType": "html", "content": content},
                “start”: {
                    "dateTime": start.strftime("%Y-%m-%dT%H:%M:%S.%f"),
                    "timeZone": "UTC",
                },
                "end": {
                    "dateTime": end.strftime("%Y-%m-%dT%H:%M:%S.%f"),
                    "timeZone": "UTC",
                },
                "location": {"displayName": location},
                "isAllDay": is_all_day,
            }
            event = create_calendar_event(token, json.dumps(payload))
            print(event)

            return render(request, self.template_name, context=event)

        return render(request, "event-form.html", {"form": form})

I am getting the access_token and the form data correctly passed into the payload dictionary, however, when I print out event I receive the following error message:

{u'error': {u'innerError': {u'date': u'2020-01-20T21:59:24', u'request-id': u'fxbxd5c1-myxx-reqx-idxx-1xxxxab91a'}, u'message': u'Empty Payload. JSON content expected.', u'code': u'BadRequest'}}

Everything is correctly set up on the Azure/Microsoft Graph side. I am able to GET my calendar events and POST requests when I use the Microsoft Graph Explorer. The problem is only with POSTing events to my calendar from my Django app.

Any thoughts on where I could be going wrong?


Solution

  • I will never understand why it takes spending the time to write a post on stackoverflow before the answer magically comes to you.

    The problem was in my post function:

    event = create_calendar_event(token, json.dumps(payload))
    

    I should be passing in a dict and not json as the payload. replacing json.dumps(payload) for payload does the trick. Event should look like this:

    event = create_calendar_event(token, payload)  # remove json.dumps from payload