如果你运行一个普通的样本.
1.GraphClient.Me.FindMeetingTimes,错误[/Me请求仅对委托身份验证流有效.]
2.GraphClient.Users["id"].FindMeetingTimes,出现[无效的用户地址]错误.
以下是结果.
当我在Graph Explorer(https://graph.microsoft.com/v1.0/users/{id}/microsoft.graph.findMeetingTimes)中运行POST时,响应返回正确,但是当我try 使用代码片段中的源代码输出时,在try 使用代码片段中的源代码输出时出现错误.
[示例代码(VSCode)]
Azure函数.Net 6(LTS)独立
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Mvc;
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Identity.Client;
namespace Company.Function
{
public class HttpTrigger0
{
private readonly ILogger _logger;
public HttpTrigger0(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<HttpTrigger5>();
}
[Function("HttpTrigger0")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestData req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
// Dummy Id
var userId = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
// https://learn.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=csharp#client-credentials-provider
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = Environment.GetEnvironmentVariable("TENANT_ID");
var clientId = Environment.GetEnvironmentVariable("CLIENT_ID");
var clientSecret = Environment.GetEnvironmentVariable("CLIENT_SECRET");
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var requestBody = new Microsoft.Graph.Me.FindMeetingTimes.FindMeetingTimesPostRequestBody
//var requestBody = new Microsoft.Graph.Users.Item.FindMeetingTimes.FindMeetingTimesPostRequestBody
{
Attendees = new List<AttendeeBase>
{
new AttendeeBase
{
EmailAddress = new EmailAddress
{
Name = "User1",
Address = "user1@test.onmicrosoft.com",
},
},
new AttendeeBase
{
EmailAddress = new EmailAddress
{
Name = "User2",
Address = "user2@test.onmicrosoft.com",
},
},
new AttendeeBase
{
EmailAddress = new EmailAddress
{
Name = "User3",
Address = "user3@test.onmicrosoft.com",
},
},
},
};
try {
var result = await graphClient.Me.FindMeetingTimes.PostAsync(requestBody);
//var result = await graphClient.Users[userId].FindMeetingTimes.PostAsync(requestBody);
return new OkObjectResult(result);
} catch (Exception e) {
Console.WriteLine("TRC:" + e.StackTrace);
Console.WriteLine("ERR:" + e.Message);
return new OkObjectResult(null);
}
}
}
}