0

Microsoft graph sdk C#: Create subscription throw error as below expirationDateTime is a required property for subscription creation

Setup of my graph client

// Values from app registration
var clientId = integration.UserName;
var tenantId = integration.TenantId; 
var clientSecret = integration.ApiKey;
// using Azure.Identity;
var options = new ClientSecretCredentialOptions
{
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};

// https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
    tenantId, clientId, clientSecret, options);

var graphClient = new GraphServiceClient(clientSecretCredential);

=====

try
{
    var webhook = new Subscription
    {
        ChangeType = "created",
        NotificationUrl = notificationURL,
        Resource = "me/messages",
        ExpirationDateTime = DateTimeOffset.Now.AddMinutes(3500), 
        ClientState = Guid.NewGuid().ToString(),
        LatestSupportedTlsVersion = "v1_2",
    };

    var webhookDataResult1 = graphClient.Subscriptions.ToPostRequestInformation(webhook);
    var webhookDataResult = await graphClient.Subscriptions.PostAsync(webhook);

    // Successful creation of the webhook
}
catch (ODataError odataError)
{
    Console.WriteLine($"Graph API error response: {odataError.Error.Code}");
    Console.WriteLine($"Graph API error response: {odataError.Error.Message}");
}

This is my setup in attached imageenter image description here

  • Could you clarify which code throws the exception? The code you pasted or the code from the screenshot? DateTimeOffset.Now.AddMinutes(3500) or DateTimeOffset.UtcNow.AddDays(7)? Please share the error's detail. Do you use delegated or application permissions? I suppose that you use application permissions and in that case Resource cannot be "me/messages" but "users/{user_id}/messages" – user2250152 Jul 08 '23 at 08:16
  • I am getting error in both code in Screenshot code and Added code both I have searched a lot and it look like we need to setup delegate access for that need to setup new graph service client but it is not supported in >net6 This is the document I found : https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/1666 that is look like helpful but still I am not able to setup this case Any help to create GraphServiceClientAsync in the documented way? – Hardik Masalawala Jul 11 '23 at 08:05
  • 1
    Found an issue and solved by following this code https://stackoverflow.com/a/76442492/6923146 – Hardik Masalawala Jul 11 '23 at 09:21

0 Answers0