Post 使用 Django、Requests 和 Microsoft Graph 将数据传输到 Outlook 日历
Post data to Outlook calendar using Django, Requests, and Microsoft Graph
我有一个功能,我尝试使用 Microsoft 的 Graph API 向我的 Outlook 日历发出 post 请求。
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()
在我看来,我正在使用表单从用户那里获取数据,这样我就可以在 return post 中将用户数据添加到我的 Outlook 日历中。表格看起来像这样:
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})
我正在获取 access_token
并将表单数据正确传递到有效负载字典中,但是,当我打印出 event
时,我收到以下错误消息:
{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'}}
Azure/Microsoft Graph 端的一切都已正确设置。当我使用 Microsoft Graph Explorer 时,我能够获取我的日历事件和 POST 请求。问题仅在于从我的 Django 应用程序 POSTing 事件到我的日历。
关于我可能哪里出错的想法?
我永远不会明白为什么在答案神奇地出现之前花时间在 Whosebug 上写一个 post。
问题出在我的 post 函数中:
event = create_calendar_event(token, json.dumps(payload))
我应该传入一个字典而不是 json 作为有效负载。将 json.dumps(payload)
替换为 payload
就可以了。事件应如下所示:
event = create_calendar_event(token, payload) # remove json.dumps from payload
实际上我认为您的变量 payload
的格式为 json。这就是为什么你不需要使用 json.dumps
.
我有一个功能,我尝试使用 Microsoft 的 Graph API 向我的 Outlook 日历发出 post 请求。
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()
在我看来,我正在使用表单从用户那里获取数据,这样我就可以在 return post 中将用户数据添加到我的 Outlook 日历中。表格看起来像这样:
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})
我正在获取 access_token
并将表单数据正确传递到有效负载字典中,但是,当我打印出 event
时,我收到以下错误消息:
{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'}}
Azure/Microsoft Graph 端的一切都已正确设置。当我使用 Microsoft Graph Explorer 时,我能够获取我的日历事件和 POST 请求。问题仅在于从我的 Django 应用程序 POSTing 事件到我的日历。
关于我可能哪里出错的想法?
我永远不会明白为什么在答案神奇地出现之前花时间在 Whosebug 上写一个 post。
问题出在我的 post 函数中:
event = create_calendar_event(token, json.dumps(payload))
我应该传入一个字典而不是 json 作为有效负载。将 json.dumps(payload)
替换为 payload
就可以了。事件应如下所示:
event = create_calendar_event(token, payload) # remove json.dumps from payload
实际上我认为您的变量 payload
的格式为 json。这就是为什么你不需要使用 json.dumps
.