How can I use flask to validate the Token for a webhook subscription?

Yaroslav 0 Reputation points
2023-10-30T17:46:57.7+00:00

Im trying to get a ms graph subscription going in my app but when I send the subscription request to:

https://graph.microsoft.com/v1.0/subscriptions

And get the follow-up request FROM ms grap to my endpoint (ValidateSubscription), flask refuses to parse it because of its incorrect mediatype which IS text/plain. So far Ive tried using flask-accept module to parse the response like this:

class ValidateSubscription(Resource):
    @accept('text/plain')
    def post:
        if flask.request.args.get("validationToken"):
            token = flask.request.args.get('validationToken')
            return Response(status=200, mimetype='text/plain', response=token)
        else:
            # process notification
            pass

but it didnt work and I got the same error.

Also Ive tried to add an api representation to my flask app like this:

@api.representation('text/plain')
def output_text(data, code, headers=None):
    resp = flask.make_response(data, code, headers)
    resp.headers.extend(headers or {})
    return resp

When I print out api.representations I see:

OrderedDict([('application/json', <function output_json at 0x7f872b021424>), ('text/plain', <function output_text at 0x7f8728d04214>)])

And I still get the same exact error without change whatsoever. Is there a better way to allow flask-restful to accept a text/plain header or am I doing something wrong? Maybe there is a way to send the token as json from the subscriptions endpoint?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
9,130 questions
0 comments No comments
{count} votes