2015-08-28 101 views
1

我正在使用adalJS对Azure AD进行身份验证的AngularJS应用程序。我试图使用Graph REST API从AD中访问登录用户的详细信息。但是我得到了401(未经授权:丢失或格式不正确的访问令牌)错误。以下是我的代码。使用adalJS访问Azure图REST API

adalAuthenticationService.acquireToken("https://graph.windows.net/").then(function (token) { 
     console.log(token); 

     var req = { 
      method: 'GET', 
      url: 'https://graph.windows.net/me?api-version=1.5', 
      headers: { 
       'Authorization': 'Bearer ' + token 
      } 
     }; 
     $http(req).then(function (response) { 
      console.log(response); 
     }, function (response) { 
      console.log(response); 
     }); 
    }); 

我在AAD中创建了应用程序,并将权限配置为Azure AD。任何人都可以详细说明这个错误?我通过的令牌不是访问令牌吗?

+0

我一直在为今天下午更好的一部分努力解决这个问题,但都无济于事。有趣的是(为了我的目的,我已经足够了),我可以从发生在客户端的auth获得令牌服务器端,并通过我的服务器代理我的API请求。它在功能上是等价的,尽管服务器有额外的负载,并且由于需要发出两个请求而导致性能下降。 –

回答

0

我很早就想出了答案。通过在初始化adalJS(在endpoints属性中)时指定Graph API端点,它正在为其获取令牌。

var endpoints = { 
     'https://graph.windows.net/': '00000002-0000-0000-c000-000000000000' 
    }; 

    adalProvider.init({ 
     'instance':'https://login.microsoftonline.com/', 
     'tenant': 'microsoft.onmicrosoft.com', 
     'clientId': '<client-id>', 

     'endpoints': endpoints 

    },$httpProvider);