2017-02-21 52 views
0

我试图将无声身份验证实现到Sharepoint Online到我的应用程序中。我已经添加了Office365连接的服务。我正在使用一个测试页面,它使用令牌和失效日期来填充2个文本框。我有以下代码(非无声AUTH)的作品就好了:身份验证上下文令牌缓存未定义

document.addEventListener('deviceready', function() { 
$(document).ready(function() { 
    var authContext = Microsoft.ADAL.AuthenticationContext; 
    authContext.createAsync("https://login.microsoftonline.com/common/") 
     .then(function (authContext) { 
      authContext.acquireTokenAsync(
       "https://my.sharepoint.com",  // Resource URI 
       "4be098f8-2184-4831-9ef7-3d17dbbef6a0",  // Client ID 
       "http://localhost:4400/services/office365/redirectTarget.html" // Redirect URI 
      ).then(function (authResult) { 
       $('#token').value = authResult.accessToken; 
       $('#expire').value = authResult.expiresOn; 
      }, function (err) { 
       console.log(err); 
      }); 
     }, function (err) { 
      console.log(err); 
     }); 
}); 
}); 

我则想实现无声AUTH使用下面的代码:当试图

document.addEventListener('deviceready', function() { 
$(document).ready(function() { 
    var authContext = Microsoft.ADAL.AuthenticationContext; 
    authContext.tokenCache.readItems().then(function (items) { 
     if (items.length > 0) { 
      authority = items[0].authority; 
      authContext = new Microsoft.ADAL.AuthenticationContext(authority); 
     } 
     authContext.acquireTokenSilentAsync("https://my.sharepoint.com", "4be098f8-2184-4831-9ef7-3d17dbbef6a0").then 
     (function (authResult) { 
      $('#token').value = authResult.accessToken; 
      $('#expire').value = authResult.expiresOn; 
     }, 
     function (authContext) { 
      authContext.acquireTokenAsync(
       "https://my.sharepoint.com",  // Resource URI 
       "4be098f8-2184-4831-9ef7-3d17dbbef6a0",  // Client ID 
       "http://localhost:4400/services/office365/redirectTarget.html" // Redirect URI 
      ).then(function (authResult) { 
       $('#token').value = authResult.accessToken; 
       $('#expire').value = authResult.expiresOn; 
      }, function (err) { 
       console.log(err); 
      }); 
     }, function (err) { 
      console.log(err); 
     } 
     ) 
    }); 
}); 
}); 

我收到错误读取令牌缓存,我得到的未定义的令牌缓存。我看过的每个示例都提到了令牌缓存,所以只是想知道它为什么会不明确?

回答

0

我需要创建async my authContext对象,然后才能访问tokenCache ...哎呦