You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#调用Microsoft Graph读取个人日历能否跳过Azure应用注册?

问题解答

是否可以跳过应用注册?

不可以,只要调用Microsoft Graph接口,不管访问对象是你自己的日历还是其他资源,都必须完成应用注册。
应用注册的本质是为你的自定义程序在Azure Active Directory中创建合法的身份标识,你现有代码中用到的clientId参数就来自应用注册生成的客户端ID,没有合法的注册凭据你无法获取API访问令牌,代码会直接报错无法运行。

现有代码的调整建议

  • 你当前配置的权限范围不全,仅User.Read无法读取日历信息,需要额外添加委托权限Calendars.Read,该权限不需要租户管理员同意,你使用自己的账号授权即可。
  • 如果你所在公司的Azure AD开启了多因素认证(MFA),你使用的UsernamePasswordCredential认证方式会直接失效,建议切换为交互式认证方案。

调整后可运行的参考代码:

var scopes = new[] { "User.Read", "Calendars.Read" };
var options = new TokenCredentialOptions
{
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};

var userName = "myaccount@company.com";
var password = "pwdhere";
// tenantId、clientId均为应用注册完成后,可在Azure AD管理页获取的参数
var tenantId = "替换为你的租户ID";
var clientId = "替换为应用注册生成的客户端ID";

var userNamePasswordCredential = new UsernamePasswordCredential(
    userName, password, tenantId, clientId, options);

var graphClient = new GraphServiceClient(userNamePasswordCredential, scopes);   

var events = await graphClient.Me.Calendar.Events
    .Request()
    .Filter("startsWith(subject,'All')")
    .GetAsync();

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 04:06:02