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

2024-06-25 06:43:51 发布

您现在位置:Python中文网/ 问答频道 /正文

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

但是,当Microsoft Graph send me 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.

我在互联网上搜索,解决方法是在设置.py文件 但是,我不能这样做,我需要附加一个斜杠

另外,另一种解决方法是在发帖时加上斜杠,然而,我不是发帖的人,微软的graph是给我发帖的人。你知道吗

这是我创建的视图,用于让micrsoft graph向我发送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,
    )

我该怎么办?你知道吗


Tags: thetoapiurlforsettingswebhookpost
1条回答
网友
1楼 · 发布于 2024-06-25 06:43:51

我不知道为什么,但它是固定后,我改变了通知的网址从 这是:

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/

唯一的区别是结尾的斜杠

相关问题 更多 >