Looks like, the problem is on Token generation
var DynamicsWebApi = require('dynamics-web-api');
var clientId = context.APPLICATION_CLIENT_ID;
var AuthenticationContext = require('adal-node').AuthenticationContext;
//OAuth Token Endpoint
var authorityUrl = 'login.microsoftonline.com/.../token';
//CRM Organization URL
var resource = 'desgrupohiberus.crm4.dynamics.com';
var secret = context.SECRET;
var adalContext = new AuthenticationContext(authorityUrl);
var tokenTemp='';
//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
tokenTemp=token.accessToken;
dynamicsWebApiCallback(token);
callback(null,tokenTemp);
}else{
console.log('Token has not been retrieved. Error: ' + error.stack);
callback(error,null);
}
}
//call a necessary function in adal-node object to get a token
adalContext.acquireTokenWithClientCredentials(resource, clientId, secret, adalCallback);
}
var dynamicsWebApi = new DynamicsWebApi({
webApiUrl: 'desgrupohiberus.crm4.dynamics.com/.../',
onTokenRefresh: acquireToken
});
//call any function
dynamicsWebApi.executeUnboundFunction("WhoAmI").then(function (response) {
console.log('Hello Dynamics 365! My id is: ' + response.UserId);
callback(null,tokenTemp);
}).catch(function(error){
console.log("FAIL!!!");
console.log(error);
callback(error,null);
});
-----------
Answer:
{ code: '0x80072560',
message: 'The user is not a member of the organization.',
status: 403,
statusMessage: 'Forbidden',
headers: '[Object]' }
------------
I double check clientId and secret
I'm a little lost
But... The most weird thing... The token it's ok