首页 文章

使用adalJS访问Azure Graph REST API

提问于
浏览
1

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

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 . 任何人都可以详细说明这个错误吗?我传递的令牌不是访问令牌吗?

2 回答

  • 0

    我很快就找到了答案 . 通过在初始化adalJS(在 endpoints 属性中)时指定Graph API endpoints ,它正在获取它的令牌 .

    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);
    
  • 0

    您需要专门为图表获取令牌 . 有关如何在ADAL js中调用酯类Web API的示例,请参阅https://github.com/AzureADSamples/SinglePageApp-WebAPI-AngularJS-DotNet

相关问题