AzureAD / azure-activedirectory-library-for-js

The code for ADAL.js and ADAL Angular has been moved to the MSAL.js repo. Please open any issues or PRs at the link below.
https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/maintenance/adal-angular
Apache License 2.0
627 stars 374 forks source link

Authentication Issue with Oatuh API #879

Closed edanhauon closed 4 years ago

edanhauon commented 5 years ago

Hi,

I'm trying to make a REST API call using DynamicsWebApi node.js framework.

Getting an 401 when trying to access the API:

message:"Get Token request returned http error: 401 and server response: {"error":"invalid_client","error_description":"AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.\r\nTrace ID: \r\nCorrelation ID: \r\nTimestamp: 2019-07-18 14:46:12Z","error_codes":[7000218],"timestamp":"2019-07-18 14:46:12Z","trace_id":"","correlation_id":""}"

Would appreciate your help.

Edan.

edanhauon commented 5 years ago

This is my code: (taken from DynamicsWebApi documentation)

var DynamicsWebApi = require('dynamics-web-api');
var AuthenticationContext = require('adal-node').AuthenticationContext;
var Q = require('q');
//the following settings should be taken from Azure for your application
//and stored in app settings file or in global variables

//OAuth Token Endpoint
var authorityUrl = '';
//CRM Organization URL
var resource = '';
//Dynamics 365 Client Id when registered in Azure
var clientId = '';
var username = '';
var password = '';

var adalContext = new AuthenticationContext(authorityUrl);

//add a callback as a parameter for your function
function acquireToken(dynamicsWebApiCallback){
    //a callback for adal-node
    function adalCallback(error, token) {
        if (!error){
            //call DynamicsWebApi callback only when a token has been retrieved
            dynamicsWebApiCallback(token);
        }
        else{
            console.log('Token has not been retrieved. Error: ' + error.stack);
        }
    }

    //call a necessary function in adal-node object to get a token
    adalContext.acquireTokenWithUsernamePassword(resource, username, password, clientId, adalCallback);
}

//create DynamicsWebApi object
var dynamicsWebApi = new DynamicsWebApi({
    webApiUrl: '',
    onTokenRefresh: acquireToken
});

//call any function
// var response = await Q.ninvoke(dynamicsWebApi, 'executeUnboundFunction', "WhoAmI");
// console.log('Hello Dynamics 365! My id is: ' + response.UserId);
dynamicsWebApi.executeUnboundFunction("WhoAmI").then(function (response) {
    console.log('Hello Dynamics 365! My id is: ' + response.UserId);
}).catch(function(error){
    console.log(error.message);
});
sameerag commented 4 years ago

Can you please migrate to msal js and let us know if it still an issue?

All current authentication work from microsoft is delivered through msal js library here. adal js is still supported only for security fixes. We would recommend to move to msal js for any advanced feature asks.