question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hello

I am trying to run the basic example but i keep getting an unexpected error:

{ Error: Unexpected Error
    at IncomingMessage.<anonymous> (/Users/araza/NodeScripts/CRM-scripts/node_modules/dynamics-web-api/lib/requests/http.js:82:33)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9) status: 401 }

Here is my code… My client id and creds are correct because i tested by following the tutorial in this blog post:

http://alexanderdevelopment.net/post/2016/11/23/dynamics-365-and-node-js-integration-using-the-web-api/

Here is my code:

var DynamicsWebApi = require('dynamics-web-api');
var AuthenticationContext = require('adal-node').AuthenticationContext;

//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 = 'https://login.windows.net<guid>/oauth2/token';
//CRM Organization URL
var resource = 'https://myorg.crm.dynamics.com';
//Dynamics 365 Client Id when registered in Azure
var clientId = '<my-client-id-here>';

var username = '<my-username>';
var password = '<my-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
  console.log('calling auth')
  adalContext.acquireTokenWithUsernamePassword(resource, username, password, clientId, adalCallback);
}


//create DynamicsWebApi object
var dynamicsWebApi = new DynamicsWebApi({
  webApiUrl: 'https://myorg.api.crm.dynamics.com/api/data/v8.2/',
  onTokenRefresh: acquireToken
});

//call any function
dynamicsWebApi.executeUnboundFunction("WhoAmI").then(function(response) {
  console.log('Hello Dynamics 365! My id is: ' + response.UserId);
}).catch(function(error) {
  console.log(error);
});

Please help. This package really looks promising and would love to use it on my project.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
swhillance-mphncommented, May 22, 2019

Ended up solving it by creating an ‘Application User’ in Dynamics 365, using the Application (Client) ID listed on the App Service in Azure. Once I did that I got status 200. Thanks Alek.

0reactions
AleksandrRogovcommented, May 22, 2019

@swhillance-mphn thanks for posting your solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"An Unexpected Error Occurred" Error Message When You ...
Cause. This issue may occur if there is low memory on the computer. As a result, the computer may lose the network bridge...
Read more >
What Should I Do if I Get an “Unexpected Error” Message - uKit
If you encounter an unexpected error while editing your website in the uKit website builder, report the problem to the Support Team.
Read more >
UnexpectedError
UnexpectedError.getErrorMessage(options). Returns the error message as a magicpen instance. The method uses the assertion error modes to produce the correct ...
Read more >
An unexpected error occurred (I-200) - Adobe Support
Read this article to troubleshoot the error, "An unexpected error occurred", in Adobe Muse.
Read more >
I received an "Unexpected error occurred" error message
Troubleshooting steps: First, try refreshing the page and signing the document again. We generally get that error when there is a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found