2
votes

I'm working on AngularJS application which authenticates the user with Azure AD using adalJS. I'm trying to access the logged in user's details from AD using the Graph REST API. But I get a 401 (Unauthorized: Missing or malformed access token) error. Below is my code.

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

I have created the application in AAD and configured the permissions to the Azure AD as well. Can anyone elaborate on this error? Is the token that I pass not the access token?

2
I've been struggling with this exact issue for the better part of this afternoon, to no avail. Interestingly (and, for my purposes, sufficiently), I was able to get a token server-side from the auth that happened client-side, and proxy my API request through my server like that. It's functionally equivalent, although there is the extra load on the server and performance hit from having to make two requests.Matthew Haugen

2 Answers

0
votes

You need to get a token specifically for the graph. For an example of how to call an ester web API in ADAL js please see https://github.com/AzureADSamples/SinglePageApp-WebAPI-AngularJS-DotNet

0
votes

I figured out the answer quite sometime back. By specifying the Graph API endpoint while initalizing adalJS(in the endpoints property), it was acquiring the token for it.

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