我正在尝试从 outlooo 365 API 获取一些日历项目
我从示例中得到的代码是这样的:
public async Task<ActionResult> Index() {
// fetch from stuff user claims
var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
var userObjectId = ClaimsPrincipal.Current.FindFirst(SettingsHelper.ClaimTypeObjectIdentifier).Value;
// discover contact endpoint
var clientCredential = new ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret);
var userIdentifier = new UserIdentifier(userObjectId, UserIdentifierType.UniqueId);
// create auth context
AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.AzureADAuthority, new EFADALTokenCache(signInUserId));
// create O365 discovery client
DiscoveryClient discovery = new DiscoveryClient(new Uri(SettingsHelper.O365DiscoveryServiceEndpoint),
async () => {
var authResult = await authContext.AcquireTokenSilentAsync(SettingsHelper.O365DiscoveryResourceId, clientCredential, userIdentifier);
return authResult.AccessToken;
});
// query discovery service for endpoint for 'calendar' endpoint
var dcr = await discovery.DiscoverCapabilityAsync("Calendar");
// create Outlook client using the calendar api endpoint
OutlookServicesClient client = new OutlookServicesClient(dcr.ServiceEndpointUri,
async () => {
var authResult = await authContext.AcquireTokenSilentAsync(dcr.ServiceResourceId, clientCredential,
userIdentifier);
return authResult.AccessToken;
});
// get contacts
var results = await client.Me.Events.Take(20).ExecuteAsync();
ViewBag.Events = results.CurrentPage.OrderBy(c => c.Start);
return View();
}
我得到这个错误:
静默获取令牌失败。调用方法 AcquireToken 描述:在执行当前 Web 请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException:无法静默获取令牌。调用方法 AcquireToken
源错误:
Line 42: OutlookServicesClient client = new OutlookServicesClient(dcr.ServiceEndpointUri,
Line 43: async () => {
Line 44: var authResult = await authContext.AcquireTokenSilentAsync(dcr.ServiceResourceId, clientCredential,
Line 45: userIdentifier);
这是设置助手
public class SettingsHelper {
public static string ClientId {
get { return ConfigurationManager.AppSettings["ida:ClientID"]; }
}
public static string ClientSecret {
get { return ConfigurationManager.AppSettings["ida:Password"]; }
}
public static string AzureAdTenantId {
get { return ConfigurationManager.AppSettings["ida:TenantId"]; }
}
public static string O365DiscoveryServiceEndpoint {
get { return "https://api.office.com/discovery/v1.0/me/"; }
}
public static string O365DiscoveryResourceId {
get { return "https://api.office.com/discovery/"; }
}
public static string AzureAdGraphResourceId {
get { return "https://graph.windows.net"; }
}
public static string AzureADAuthority {
get { return string.Format("https://login.windows.net/{0}/", AzureAdTenantId); }
}
public static string ClaimTypeObjectIdentifier {
get { return "http://schemas.microsoft.com/identity/claims/objectidentifier"; }
}
}
我很确定 clientid 和 secret 没问题,因为我已经在 azure AD 上创建了应用程序。