I am trying to get some calendar items from outlooo 365 API
The code I got from the sample is this:
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();
}
This is based on the sample provided here> https://github.com/OfficeDev/TrainingContent/tree/master/O3651/O3651-5%20Getting%20started%20with%20Office%20365%20APIs/Completed%20Projects
And I get this error:
Failed to acquire token silently. Call method AcquireToken Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException: Failed to acquire token silently. Call method AcquireToken
Source Error:
Line 42: OutlookServicesClient client = new OutlookServicesClient(dcr.ServiceEndpointUri,
Line 43: async () => {
Line 44: var authResult = await authContext.AcquireTokenSilentAsync(dcr.ServiceResourceId, clientCredential,
Line 45: userIdentifier);
and this is the settingshelper
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"; }
}
}
I am pretty sure the clientid and secret are OK as I already created the application on azure AD.
I compared the settings helper with the one in my project, which authenticates correctly. The only difference is you have and extra '/' at the end of the AzureADAuthority property.
public static string AzureADAuthority {
get {return string.Format("https://login.windows.net/{0}", AzureAdTenantId); }
}
As a tip, I got the same error when I was trying to implement Video REST API demo app using VS Community 2015. I could fix it updating all packages, I noticed there was particular one related to Microsoft identities or something like that, maybe that package was causing the error.
When I used the same code using VS Community 2013 Update 4 I didn't have any issue.
Regards
Unfortunately it was a bug on vs 2015 RC, on the RTM version same code worked perfectly fine.
thanks
来源:https://stackoverflow.com/questions/31632507/failed-to-acquire-token-silently-call-method-acquiretoken