// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Models;
var requestBody = new Subscription
{
ChangeType = "created,updated",
NotificationUrl = "https://webhook.azurewebsites.net/api/resourceNotifications",
LifecycleNotificationUrl = "https://webhook.azurewebsites.net/api/lifecycleNotifications",
Resource = "/users/{id}/messages",
ExpirationDateTime = DateTimeOffset.Parse("2020-03-20T11:00:00.0000000Z"),
ClientState = "<secretClientState>",
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Subscriptions.PostAsync(requestBody);
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
Subscription subscription = new Subscription();
subscription.setChangeType("created,updated");
subscription.setNotificationUrl("https://webhook.azurewebsites.net/api/resourceNotifications");
subscription.setLifecycleNotificationUrl("https://webhook.azurewebsites.net/api/lifecycleNotifications");
subscription.setResource("/users/{id}/messages");
OffsetDateTime expirationDateTime = OffsetDateTime.parse("2020-03-20T11:00:00.0000000Z");
subscription.setExpirationDateTime(expirationDateTime);
subscription.setClientState("<secretClientState>");
Subscription result = graphClient.subscriptions().post(subscription);
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.models.subscription import Subscription
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = Subscription(
change_type = "created,updated",
notification_url = "https://webhook.azurewebsites.net/api/resourceNotifications",
lifecycle_notification_url = "https://webhook.azurewebsites.net/api/lifecycleNotifications",
resource = "/users/{id}/messages",
expiration_date_time = "2020-03-20T11:00:00.0000000Z",
client_state = "<secretClientState>",
)
result = await graph_client.subscriptions.post(request_body)
//The following code is for illustrative purposes only
var TokenTimeToExpirationInMinutes=(TokenExpirationTime-CurrentTime)/4;
if((TokenTimeToExpirationInMinutes)<=180 && TokenTimeToExpirationInMinutes>60){
//Microsoft Graph will send reauthorizationRequired notification
TokenTimeToExpirationInMinutes=TokenTimeToExpirationInMinutes/2;
}
elseif(TokenTimeToExpirationInMinutes<60 && TokenTimeToExpirationInMinutes>=0){
//Microsoft Graph will send reauthorizationRequired notification every 15 mins
TokenTimeToExpirationInMinutes=TokenTimeToExpirationInMinutes-15;
} else {
//Microsoft Graph will stop sending reauthorizationRequired notifications
}
以下步骤代表活动的订阅的授权质询流:
Microsoft Graph 需要重新授权的订阅。
原因可能因资源而异,并可能随时间而变化。 若要维护订阅,必须响应重新授权事件,无论其原因如何。
Microsoft Graph 向 lifecycleNotificationUrl 发送授权质询通知。