图 API OneDrive 的 Webhook 订阅不工作
Graph API Webhook subscription for OneDrive not working
我正在尝试使用 Graph 订阅 API 在 One Drive 中捕获一个 "document share" 事件。我可以在我的 Dotnet WebAPI 应用程序中创建订阅。以下是我的代码:
Microsoft.Graph.User user = funGetO365User("user1@mydomain.onmicrosoft.com");
//Notification URL = /notification/listen
Subscription subscription = new Subscription
{
Resource = "/"+ user.Id + "/drive/root",
ChangeType = "updated",
NotificationUrl =
ConfigurationManager.AppSettings["ida:NotificationUrl"],
ClientState = Guid.NewGuid().ToString(),
//ExpirationDateTime = DateTime.UtcNow + new TimeSpan(0, 0, 4230, 0) // current maximum timespan
ExpirationDateTime = DateTime.UtcNow + new TimeSpan(0, 0, 15, 0) // shorter duration useful for testing
};
string contentString = JsonConvert.SerializeObject(subscription,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, subscriptionsEndpoint);
request.Content = new StringContent(contentString, System.Text.Encoding.UTF8, "application/json");
// Send the `POST subscriptions` request and parse the response.
GraphHttpClient graphHttpClient = new GraphHttpClient(accessToken);
HttpResponseMessage response = await graphHttpClient.SendAsync(request);
我的应用程序在本地主机上 运行,我正在使用 ngrok 将请求中继到本地主机。
创建订阅后我会收到通知:
[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");
}
else
{
// My code- Never executed
}
}
但是,如果我对用户的一个驱动器进行任何更改(编辑文档、共享文档),我不会收到任何通知。
知道我需要做什么才能只收到通知吗?
问题似乎已解决。端点 URL
"graph.microsoft.com/v1.0/drives" + user.Id + "/root"
其中 user.Id 是 Graph 用户的唯一 ID。我现在可以收到任何更改的通知,但是我想知道是否有一种方法可以仅在任何驱动器文件的权限发生更改时通知我?
我正在尝试使用 Graph 订阅 API 在 One Drive 中捕获一个 "document share" 事件。我可以在我的 Dotnet WebAPI 应用程序中创建订阅。以下是我的代码:
Microsoft.Graph.User user = funGetO365User("user1@mydomain.onmicrosoft.com");
//Notification URL = /notification/listen
Subscription subscription = new Subscription
{
Resource = "/"+ user.Id + "/drive/root",
ChangeType = "updated",
NotificationUrl =
ConfigurationManager.AppSettings["ida:NotificationUrl"],
ClientState = Guid.NewGuid().ToString(),
//ExpirationDateTime = DateTime.UtcNow + new TimeSpan(0, 0, 4230, 0) // current maximum timespan
ExpirationDateTime = DateTime.UtcNow + new TimeSpan(0, 0, 15, 0) // shorter duration useful for testing
};
string contentString = JsonConvert.SerializeObject(subscription,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, subscriptionsEndpoint);
request.Content = new StringContent(contentString, System.Text.Encoding.UTF8, "application/json");
// Send the `POST subscriptions` request and parse the response.
GraphHttpClient graphHttpClient = new GraphHttpClient(accessToken);
HttpResponseMessage response = await graphHttpClient.SendAsync(request);
我的应用程序在本地主机上 运行,我正在使用 ngrok 将请求中继到本地主机。
创建订阅后我会收到通知:
[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");
}
else
{
// My code- Never executed
}
}
但是,如果我对用户的一个驱动器进行任何更改(编辑文档、共享文档),我不会收到任何通知。
知道我需要做什么才能只收到通知吗?
问题似乎已解决。端点 URL
"graph.microsoft.com/v1.0/drives" + user.Id + "/root"
其中 user.Id 是 Graph 用户的唯一 ID。我现在可以收到任何更改的通知,但是我想知道是否有一种方法可以仅在任何驱动器文件的权限发生更改时通知我?