我正在尝试在Microsoft azure中注册我的Web应用程序,以获取有关用户一键式更改的通知。为此,我需要注册我的应用以获取通知。
我一直在遵循以下说明:Create subscription
此处Handling webhook validation requests
但是我需要一些有关Webhook验证的帮助。 创建新订阅后,OneDrive将以以下格式将请求发布到注册的URL(我的Web应用程序,或者我将使用azure函数):
POST https://contoso.azurewebsites.net/your/webhook/service?validationtoken={randomString}
Content-Length: 0
要成功创建订阅,我的服务必须通过以纯文本响应的形式返回validationtoken查询字符串参数的值来响应此请求。
HTTP/1.1 200 OK
Content-Type: text/plain
{randomString}
我正在使用django,如何按照他们的要求在python中做出响应?
这是我发送第一个帖子请求的方式
def create_subscription(token):
payload = {
"changeType": "updated",
"notificationUrl": notification_url,
"resource": "/me/drive/root",
"expirationDateTime": "2030-01-01T11:23:00.000Z",
"clientState": "client-specific string"
}
headers = {
"Authorization": token['access_token'],
"Host": "graph.microsoft.com",
"Content-Type": "application/json"
}
response = requests.post("https://graph.microsoft.com/v1.0/subscriptions".format(graph_url),
data=json.dumps(payload), headers=headers)
这是我如何响应Microsoft的POST请求
def validate_subscription(request):
if request.method == 'POST':
url = request.get_full_path()
parsed = urlparse.urlparse(url)
validation_string = parsed.query['validationtoken']
headers = {
"Host": "graph.microsoft.com",
"Content-Type": "text/plain"
}
r = requests.post("{0}/subscriptions".format(graph_url),
data=validation_string, headers=headers)
if (r.status_code == 200):
return HttpResponse("Subscription suceeded")
我们不断收到表示“验证错误”的错误
谢谢你们
答案 0 :(得分:1)
您不应该向Graph发送一个POST
请求,您应该使用传入的令牌来响应 Graph的请求:
POST
发送到您的通知端点。您的通知端点通过以下方式响应POST
:
def validate_subscription(request):
if request.method == 'POST':
url = request.get_full_path()
parsed = urlparse.urlparse(url)
validation_string = parsed.query['validationtoken']
return HttpResponse(validation_string, content_type="text/plain")
答案 1 :(得分:0)
所以我要做的是将大写的'v'用作'validationtoken',因此请求应如下所示:
def validate_subscription(request):
if request.method == 'POST':
url = request.get_full_path()
parsed = urlparse.urlparse(url)
validation_string = parsed.query['Validationtoken']
headers = {
"Host": "graph.microsoft.com",
"Content-Type": "text/plain"
}
r = requests.post("{0}/subscriptions".format(graph_url),
data=validation_string, headers=headers)
if (r.status_code == 200):
return HttpResponse("Subscription suceeded")