2015-04-30 91 views
0

到目前为止,我有这个。办公室365休息Api有问题获取访问令牌

 public static async Task<OutlookServicesClient> CreateOutlookClientAsync(string capability) 
 
     { 
 
      try 
 
      { 
 

 
       string authority = CommonAuthority; 
 

 
       // Create an AuthenticationContext using this authority. 
 
       _authenticationContext = new AuthenticationContext(authority); 
 

 
       //See the Discovery Service Sample (https://github.com/OfficeDev/Office365-Discovery-Service-Sample) 
 
       //for an approach that improves performance by storing the discovery service information in a cache. 
 
       DiscoveryClient discoveryClient = new DiscoveryClient(
 
        async() => await GetTokenHelperAsync(_authenticationContext, DiscoveryResourceId)); 
 

 
       // Get the specified capability ("Contacts"). 
 
       CapabilityDiscoveryResult result = 
 
        await discoveryClient.DiscoverCapabilityAsync(capability); 
 
       var client = new OutlookServicesClient(
 
        result.ServiceEndpointUri, 
 
        async() => 
 
         await GetTokenHelperAsync(_authenticationContext, result.ServiceResourceId)); 
 
       return client; 
 
      } 
 
      catch (Exception e) 
 
      { 
 
       Console.WriteLine(e.Message); 
 
       if (_authenticationContext != null && _authenticationContext.TokenCache != null) 
 
        _authenticationContext.TokenCache.Clear(); 
 
       return null; 
 
      } 
 
     } 
 

 
    }

private static async Task<string> GetTokenHelperAsync(AuthenticationContext context, string resourceId) 
 
     { 
 
      string accessToken = null; 
 
      AuthenticationResult result = null; 
 
      string myId = WebConfigurationManager.AppSettings["ida:ClientID"]; 
 
      string myKey = WebConfigurationManager.AppSettings["ida:Password"]; 
 
      ClientCredential client = new ClientCredential(myId,myKey); 
 
      
 
      result = await context.AcquireTokenAsync(resourceId, client); 
 
      //result =context.AcquireToken(resourceId, ClientID,_returnUri); 
 
      accessToken = result.AccessToken; 
 

 

 
      return accessToken; 
 
     }

,当我到达导致两件事情发生,如果我的用户AcquireTokenAsync我得到一个错误,说明与标识XXXX应用程序目录未找到api.office.com否则,如果我运行AcquireToken我得到登录模式弹出,但发生错误指示请求必须包含client_secret。

我不知道如何解决这个问题我怀疑它可能与实际的应用程序配置有关我已经尝试在Azure AD中创建我自己的应用程序并使用VS连接服务,有任何其他人遇到类似问题是什么?

回答

1

根据您所看到的错误,您的应用注册方式似乎存在问题。第一个错误通常发生在未将应用程序标记为多租户时,并且您以与注册应用程序的租户不同的租户的身份登录到应用程序。

第二个错误是奇怪的。客户端秘密是您正在读取ida:Password元素并传入ClientCredential对象的内容。

我昨天刚刚放了一个.NET tutorial,通过设置这些东西。仔细观察一下,看看是否有助于您畅通无阻。

+0

谢谢杰森。我用你的教程来获得一个认证令牌,但现在我得到一个空引用异常来自mscorlib.dll – Frank

+0

感谢杰森的教程!我使用了O365 Unified API :)现在我已经将它与我的MVC应用程序集成在一起了! –