Django在维护POST数据的同时无法重定向到斜杠URL(Microsoft Graph webhook)

时间:2019-06-11 02:45:55

标签: python django-rest-framework microsoft-graph

我正在尝试为Microsoft图形制作一个Webhook。 (文档在这里:https://docs.microsoft.com/en-us/graph/webhooks

但是,当Microsoft Graph向我发送POST时,它会向我发送以下URL:

9d065f52.ngrok.io/api/v1.0/user-calendar-settings/calendar-webhook/?validationToken=Validation%3a+Testing+client+application+reachability+for+subscription+Request-Id%3a+c340fcfe-a079-4dd8-85c5-8dc10c158250

但是由于没有斜杠,所以我得到了这个错误:

RuntimeError: You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to 9d065f52.ngrok.io/api/v1.0/user-calendar-settings/calendar-webhook/?validationToken=Validation%3a+Testing+client+application+reachability+for+subscription+Request-Id%3a+c340fcfe-a079-4dd8-85c5-8dc10c158250 (note the trailing slash), or set APPEND_SLASH=False in your Django settings.

我在互联网上搜索,解决方法是更改​​settings.py文件中的APPEND_SLASH设置 但是,我不能这样做,我需要APPEND_SLASH

另外,其他解决方案是在POST时仅添加斜杠,但是,我不是POST的人,Microsoft图形是向我发送POST的人。

这是我创建的用于让micrsoft图将POST发送给我的视图:

# /api/v1.0/user-calendar-settings/calendar-webhook/
@action(
    detail=False,
    methods=["post"],
    serializer_class=EmptySerializer,
    url_path="calendar-webhook",
    permission_classes=[AllowAny],
)
def calendar_webhook(self, request, pk=None):
    """
    Notification endpoint validation for microsoft graph
    """

    print(request.body)

    validation_token = request.POST.get("validationToken", "")
    import jwt

    # jwt.decode(<encoded token>,<secret key>,<algorthm>)
    decodedPayload = jwt.decode(validation_token, None, None)
    return Response(
        {"status": "ok"},
        status=status.HTTP_200_OK,
        content_type="text/plain",
        data=decodedPayload,
    )

我该怎么办?

1 个答案:

答案 0 :(得分:0)

我不知道为什么,但是在我更改了notification_url之后,它已修复 这个:

notification_url = 9d065f52.ngrok.io/api/v1.0/user-calendar-settings/calendar-webhook

至:

notification_url = 9d065f52.ngrok.io/api/v1.0/user-calendar-settings/calendar-webhook/

唯一的区别是最后的斜杠