[英]Azure Microsoft Graph API - Subscription - validation request failed
I try to set up a notification for changes in Azure user data ( https://docs.microsoft.com/en-us/graph/webhooks ).我尝试为 Azure 用户数据的更改设置通知 ( https://docs.microsoft.com/en-us/graph/webhooks )。
I send a request from a local client to the Azure Graph API and I have a publicly available server (with SSL) in the payload of the request as notification URL.我从本地客户端向 Azure Graph API 发送请求,并且在请求的有效负载中有一个公开可用的服务器(带有 SSL)作为通知 URL。
Azure now sends a posts request to my server (like in the documentation - exact post request see below) and I try to send the token I got back (like in the documentation). Azure 现在向我的服务器发送一个帖子请求(就像在文档中一样 - 确切的帖子请求见下文),我尝试发送我得到的令牌(就像在文档中一样)。 But I always get the following error message "Subscription validation request failed. Response must exactly match validationToken query parameter."
但我总是收到以下错误消息“订阅验证请求失败。响应必须与validationToken 查询参数完全匹配。”
Post request from Azure:来自 Azure 的发布请求:
Path: /?validationToken=Validation%3a+Testing+client+application+reachability+for+subscription+Request-Id%3a+3b3f9821-ce3f-23d9-879b-00a23f3 Body: is empty
I tried every part and encoding of the path (like just the request ID or the whole path) but I always get the error message.我尝试了路径的每个部分和编码(例如请求 ID 或整个路径),但我总是收到错误消息。 So whats the right thing to send back?
那么什么是正确的送回去呢?
Firstly, the validation token you receive should be treated as an opaque value and returned unchanged and the error Subscription validation request failed. Response must exactly match validationToken query parameter
首先,您收到的验证令牌应被视为不透明值并原样返回,并且错误
Subscription validation request failed. Response must exactly match validationToken query parameter
Subscription validation request failed. Response must exactly match validationToken query parameter
is trying to tell you that something changed. Subscription validation request failed. Response must exactly match validationToken query parameter
试图告诉您某些内容发生了变化。
Since the validation token comes to you as a URL query parameter, make sure you're working with a properly decoded value in your code before returning it .由于验证令牌是作为 URL 查询参数提供给您的,因此请确保在返回之前在代码中使用正确解码的值。
Here is the relevant documentation from Microsoft Docs: Notification endpoint validation以下是 Microsoft Docs 的相关文档: 通知端点验证
POST https://{notificationUrl}?validationToken={opaqueTokenCreatedByMicrosoftGraph}
Other requirements(from the same reference):其他要求(来自同一个参考):
Code Samples代码示例
ASP.NET MVC Sample - Specifically look at the NotificationController.cs file ASP.NET MVC 示例- 具体看 NotificationController.cs 文件
[HttpPost]
public async Task<ActionResult> Listen()
{
// Validate the new subscription by sending the token back to Microsoft Graph.
// This response is required for each subscription.
if (Request.QueryString["validationToken"] != null)
{
var token = Request.QueryString["validationToken"];
return Content(token, "plain/text");
}
Node.js code sample - Specifically look at listen.js Node.js 代码示例——具体看listen.js
/* Default listen route */
listenRouter.post('/', (req, res, next) => {
let status;
let clientStatesValid;
// If there's a validationToken parameter in the query string,
// then this is the request that Office 365 sends to check
// that this is a valid endpoint.
// Just send the validationToken back.
if (req.query && req.query.validationToken) {
res.send(req.query.validationToken);
// Send a status of 'Ok'
status = 200;
}
You should return the validationToken
from the query string with an HTTP 200
response code.您应该从带有
HTTP 200
响应代码的查询字符串中返回validationToken
。 You also have to do that within a few seconds, or the graph will fail the request and your call to create the subscription will fail.您还必须在几秒钟内执行此操作,否则图形将无法请求,并且您创建订阅的调用将失败。
Here is an example of the validation endpoint in ASP.NET Web API 2:以下是 ASP.NET Web API 2 中验证端点的示例:
public ActionResult<string> Post([FromQuery]string validationToken = null)
{
if(!string.IsNullOrEmpty(validationToken))
{
Console.WriteLine($"Token: '{validationToken}'");
return Ok(validationToken);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.