How can I use FindMeetingTimes in the Graph API (SDK 5.35) from Azure Function to retrieve available appointments?

夏目 貴明 5 Reputation points
2023-12-08T09:48:59.8766667+00:00

How can I use FindMeetingTimes in the Graph API (SDK 5.35) from Azure Function to retrieve available appointments?

If you run a common sample, you will get

GraphClient.Me.FindMeetingTimes, [/me request is only valid with delegated authentication flow.] error

If GraphClient.Users["id"].FindMeetingTimes, [Invalid user address] error

The following is the result.

Outlook
Outlook
A family of Microsoft email and calendar products.
4,331 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,808 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. 夏目 貴明 5 Reputation points
    2023-12-12T11:33:16.7133333+00:00

    Sorry, I was multi-posting.

    I got the answer on StackOverFlow.

    The authentication method was changed to the Interactive provider method and the Azure configuration was changed to accommodate this.

    1 person found this answer helpful.

  2. AsithwMSFT 1,440 Reputation points Microsoft Vendor
    2023-12-09T23:35:02.7466667+00:00

    Updated - Client credential flow can be implemented like this

    api reference.

    please refer the CarlZhao-MSFT answer


  3. CarlZhao-MSFT 44,761 Reputation points
    2023-12-11T03:00:04.2133333+00:00

    Hi @夏目 貴明

    Do not use the client credentials flow for this API as it does not support application permissions. You should request the access token using a delegated authentication flow (auth code flow or ROPC flow) with user participation.

    User's image

    Code example:

    // Dependencies
    using Microsoft.Graph.Me.FindMeetingTimes;
    using Microsoft.Graph.Models;
    using Microsoft.Graph;
    using Azure.Identity;
    
    var scopes = new[] { "Calendars.ReadWrite.Shared" };
    
    var tenantId = "TENANT_ID";
    
    // Values from app registration
    var clientId = "YOUR_CLIENT_ID";
    var clientSecret = "YOUR_CLIENT_SECRET";
    
    // For authorization code flow, the user signs into the Microsoft
    // identity platform, and the browser is redirected back to your app
    // with an authorization code in the query parameters
    var authorizationCode = "AUTH_CODE_FROM_REDIRECT";
    
    // using Azure.Identity;
    var options = new AuthorizationCodeCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
    };
    
    // https://learn.microsoft.com/dotnet/api/azure.identity.authorizationcodecredential
    var authCodeCredential = new AuthorizationCodeCredential(
        tenantId, clientId, clientSecret, authorizationCode, options);
    
    var graphClient = new GraphServiceClient(authCodeCredential, scopes);
    
    // Code snippets are only available for the latest version. Current version is 5.x
    var requestBody = new FindMeetingTimesPostRequestBody
    {
    	Attendees = new List<AttendeeBase>
    	{
    		new AttendeeBase
    		{
    			Type = AttendeeType.Required,
    			EmailAddress = new EmailAddress
    			{
    				Name = "Alex Wilbur",
    				Address = "alexw@contoso.onmicrosoft.com",
    			},
    		},
    	},
    	LocationConstraint = new LocationConstraint
    	{
    		IsRequired = false,
    		SuggestLocation = false,
    		Locations = new List<LocationConstraintItem>
    		{
    			new LocationConstraintItem
    			{
    				ResolveAvailability = false,
    				DisplayName = "Conf room Hood",
    			},
    		},
    	},
    	TimeConstraint = new TimeConstraint
    	{
    		ActivityDomain = ActivityDomain.Work,
    		TimeSlots = new List<TimeSlot>
    		{
    			new TimeSlot
    			{
    				Start = new DateTimeTimeZone
    				{
    					DateTime = "2023-12-11T09:00:00",
    					TimeZone = "Pacific Standard Time",
    				},
    				End = new DateTimeTimeZone
    				{
    					DateTime = "2023-12-11T17:00:00",
    					TimeZone = "Pacific Standard Time",
    				},
    			},
    		},
    	},
    	IsOrganizerOptional = false,
    	MeetingDuration = TimeSpan.Parse("PT1H"),
    	ReturnSuggestionReasons = true,
    	MinimumAttendeePercentage = 100d,
    };
    
    var result = await graphClient.Me.FindMeetingTimes.PostAsync(requestBody, (requestConfiguration) =>
    {
    	requestConfiguration.Headers.Add("Prefer", "outlook.timezone=\"Pacific Standard Time\"");
    });
    

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.