Office 365 API Microsoft Graph 身份验证失败
Office 365 APIs Microsoft Graph authentication failed
注册了示例应用
标准登录正常,但当我尝试使用此代码使其更简单时:
var authContext = new AuthenticationContext(Settings.AzureADAuthority);
var token = authContext.AcquireToken(Settings.O365UnifiedAPIResource, new ClientCredential(Settings.ClientId, Settings.ClientSecret)).AccessToken;
我收到以下错误:Application with identifier '[ClientId here]' was not found in the directory microsoft.com
Setting.O365UnifiedAPIResource = @"https://graph.microsoft.com/";
Settings.AzureADAuthority = @"https://login.microsoftonline.com/common";
有谁知道可能是什么问题?
Settings.AzureADAuthority = @"https://login.microsoftonline.com/{tenant_id or tenant_name}";
使用客户端凭据(客户端id + 客户端密码)获取令牌时。您应该明确指定租户。
例如:
https://login.microsoftonline.com/{tenant_id}
或
https://login.microsoftonline.com/{your_domain.onmicrosoft.com}
顺便说一句,由于此注册将用于示例应用程序,因此它只有 Mail.Send 权限,即委派权限。要获取应用令牌,您还需要在 Azure AD 中授予应用级别权限,因为您获取的是应用令牌而不是用户令牌。
标准登录正常,但当我尝试使用此代码使其更简单时:
var authContext = new AuthenticationContext(Settings.AzureADAuthority);
var token = authContext.AcquireToken(Settings.O365UnifiedAPIResource, new ClientCredential(Settings.ClientId, Settings.ClientSecret)).AccessToken;
我收到以下错误:Application with identifier '[ClientId here]' was not found in the directory microsoft.com
Setting.O365UnifiedAPIResource = @"https://graph.microsoft.com/";
Settings.AzureADAuthority = @"https://login.microsoftonline.com/common";
有谁知道可能是什么问题?
Settings.AzureADAuthority = @"https://login.microsoftonline.com/{tenant_id or tenant_name}";
使用客户端凭据(客户端id + 客户端密码)获取令牌时。您应该明确指定租户。
例如:
https://login.microsoftonline.com/{tenant_id}
或
https://login.microsoftonline.com/{your_domain.onmicrosoft.com}
顺便说一句,由于此注册将用于示例应用程序,因此它只有 Mail.Send 权限,即委派权限。要获取应用令牌,您还需要在 Azure AD 中授予应用级别权限,因为您获取的是应用令牌而不是用户令牌。