2
votes

I am writing a Nodejs CLI to Deploy AAD-Auth Protected Web API via Azure ARM template. Facing issues in getting Access-Token to invoke the Web API.

I have used adal-node library to generate access token using AAD-App clientID and secret, but the generated token doesn't have valid objectId.

I also tried to get device_code and then tried to acquire access-token with same, but failed with error "needs permission to access resources in your organization that only an admin can grant. Please ask an admin to grant permission to this app before you can use it."..

However, if I go to the browser and enter https://app-service-name.azurewebsites.net/.auth/me, i am prompted to enter my azure login credentials and consent to let AAD-App access the resources, I am getting Access-token on the browser. And this access-token has all the properties (audience, issuer, objectId) i need. I am struggling to acquire similar access-token from nodejs CLI.

const context = new AuthenticationContext('https://login.microsoftonline.com/my-tenant-id');
context.acquireTokenWithClientCredentials(
    aadAppIdUri, aadAppId, aadClientSecret, (err: Error, result: any) => {
        if (err) {
            reject(err);
        } else {
            resolve(result.accessToken);
        }
    });

I want to invoke the AAD-Protected web api from my Node CLI with valid Access-Token. Kindly help me generate the Access-Token for this.

1

1 Answers

0
votes

The way you get the access token via client credentials need application permission.

The way you get the access token via login credentials need delegated permission.

Make sure you have the needed application permission for your app. enter image description here

Also, remember to click grant admin consent button.

enter image description here