未能获得默默令牌。调用方法AcquireToken [英] Failed to acquire token silently. Call method AcquireToken

查看:1454
本文介绍了未能获得默默令牌。调用方法AcquireToken的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从outlooo 365 API一些日历项

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();
    }

这是基于这里提供的示例>
https://开头github.com/OfficeDev/TrainingContent/tree/master/O3651/O3651-5%20Getting%20started%20with%20Office%20365%20APIs/Completed%20Projects

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

和我得到这个错误:

无法获得令牌默默。调用方法AcquireToken
说明:当前Web请求的执行过程中发生未处理的异常。请检查堆栈跟踪有关该错误它起源于代码的更多信息和。

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.

异常详细信息:Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException:未能收购默默令牌。调用方法AcquireToken

Exception Details: Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException: Failed to acquire token silently. Call method 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

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



我敢肯定的clientid和秘密都OK因为我已经创建了蔚蓝的AD应用程序。

I am pretty sure the clientid and secret are OK as I already created the application on azure AD.

推荐答案

不幸的是它是在VS 2015 RC的一个bug,在RTM版本相同代码工作完全正常。

Unfortunately it was a bug on vs 2015 RC, on the RTM version same code worked perfectly fine.

感谢

这篇关于未能获得默默令牌。调用方法AcquireToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆