IPublicClientApplication publicClientApplication = PublicClientApplicationBuilder
.Create(clientId)
.WithTenantId(Tenant)
.Build();
//通过username password 获取token
var token = publicClientApplication.AcquireTokenByUsernamePassword(scopes, "[username]", ReadPassword()).ExecuteAsync().Result;
//将token 打包成assertion
var assertion = GetUserAssertion(token.AccessToken);
UsernamePasswordProvider authenticationProvider = new UsernamePasswordProvider(publicClientApplication, scopes);
GraphServiceClient graphServiceClient = new GraphServiceClient(authenticationProvider);
await graphServiceClient.Me.Events
.Request()
//通过sdk 请求时需要携带 assertion
.WithUserAssertion(assertion)
.Header("Prefer", "outlook.timezone=\"Pacific Standard Time\"")
.AddAsync(@event);
//assertion 打包方法
private UserAssertion GetUserAssertion(string token)
{
return new UserAssertion(token, UserAssertionType);
}
//password 生成方法
private static SecureString ReadPassword()
{
SecureString password = new SecureString();
var pwd = "[password]";
foreach (var c in pwd)
{
password.AppendChar(c);
}
return password;
}
//@event 结构
var @event = new Event
{
Subject = "Let's go for lunch",
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = "Does noon work for you?"
},
Start = new DateTimeTimeZone
{
DateTime = "2020-10-15T12:00:00",
TimeZone = "Pacific Standard Time"
},
End = new DateTimeTimeZone
{
DateTime = "2020-10-15T14:00:00",
TimeZone = "Pacific Standard Time"
},
Location = new Location
{
DisplayName = "Harry's Bar"
},
Attendees = new List<Attendee>()
{
new Attendee
{
EmailAddress = new EmailAddress
{
Address = "[email]",
Name = "[display name]"
},
Type = AttendeeType.Required
}
},
Attachments = new EventAttachmentsCollectionPage { },
AllowNewTimeProposals = true,
TransactionId = "7E163156-7762-4BEB-A1C6-729EA81755R7",
};
适用 Microsoft Graph SDK
结合Azure AD 调用 Microsoft Graph API ,适用 SDK的时候权限认证这块比较头疼。示例的demo分别是Azure AD 的 和 Graph SDK 的, 在这里将两者结合的一个用例展示出来
本人专业开发Microsoft 技术栈程序,有问题可以联系微信:geek_edu