无法以静默方式获取令牌。调用方法AcquireToken

时间:2015-07-26 00:45:14

标签: c# office365 office365-apps outlook-restapi

我正在尝试从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();
    }

这是基于此处提供的样本&gt; https://github.com/OfficeDev/TrainingContent/tree/master/O3651/O3651-5%20Getting%20started%20with%20Office%20365%20APIs/Completed%20Projects

我收到了这个错误:

无法以静默方式获取令牌。调用方法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);

这是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"; }
    }
  }

我很确定clientid和secret是可以的,因为我已经在azure AD上创建了应用程序。

3 个答案:

答案 0 :(得分:3)

我将设置帮助器与我的项目中的设置帮助器进行了比较,该设置正确地进行了身份验证。唯一的区别是您在AzureADAuthority属性的末尾有额外的'/'。

public static string AzureADAuthority {
  get {return string.Format("https://login.windows.net/{0}", AzureAdTenantId); }
}

答案 1 :(得分:1)

作为提示,当我尝试使用VS Community 2015实现Video REST API演示应用程序时,我遇到了同样的错误。我可以修复它更新所有软件包,我注意到有一个特定的与微软身份有关或类似的东西,也许那个包导致了错误。

当我使用VS Community 2013 Update 4使用相同的代码时,我没有任何问题。

此致

答案 2 :(得分:0)

不幸的是,这是vs 2015 RC的一个错误,在RTM版本上,相同的代码完全正常。

感谢