Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Getting 401 when accessing the web API with valid token

(1) ShareShare
ReportReport
Posted on by Microsoft Employee

I am trying to use the CRM web API from a node server side script. I am able to get the token but when I make a call to the API with that token I get a 401. What am I doing wrong here.

Here is my node code:

var AuthenticationContext = require('adal-node').AuthenticationContext;
var o = require('odata');
var request = require('request');

var authorityHostUrl = 'https://login.windows.net';
var tenant = '<my-organization>.onmicrosoft.com';
var authorityUrl = authorityHostUrl + '/' + tenant;
var clientId = 'myClientId';
var clientSecret = 'azureCustomeAppSecret'
var resource = 'https://<my-organization>.crm.dynamics.com';

var context = new AuthenticationContext(authorityUrl);

context.acquireTokenWithClientCredentials(resource, clientId, clientSecret, function(err, res) {
  if (err) {
    console.log('well that didn\'t work: ' + err.stack);
  } else {
      
    console.log(res);

    request({method: 'GET', url: 'https://<my-organization>.api.crm.dynamics.com/api/data/v8.1/contacts', headers: {'OData-MaxVersion': '4.0', 'OData-Version': '4.0', 'Accept': 'application/json', 'Authorization': 'Bearer ' + res.accessToken}}, function(error, response, body) {
        if(error) {
            console.error(error)
        }
        else {
            console.log(body);
        }
    });
  }
});


 When I run this I get an access token back, but the API call creates a 401

The Azure application has rights to the CRM:

Active_5F00_Directory_5F002D005F00_Microsoft_5F00_Azure.png

 

Any ideas? 

*This post is locked for comments

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Getting 401 when accessing the web API with valid token

    Hi,

    Please add SecurityProtocol  Tls12.

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    You can refer my answer in below post-

    community.dynamics.com/.../782284

    Hope this helps.

  • mppowe Profile Picture
    mppowe 70 on at
    RE: Getting 401 when accessing the web API with valid token

    I'm having the same issue... did you ever figure out why you got the 401?  I even tried logging into CRM with the same credentials, then pointing the browser to the api endpoint and it returned a JSON of the contact results just fine.  So the user seems to have proper access.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting 401 when accessing the web API with valid token

    Hi cbrewer,

    Have you worked out why it is failing?

    Thanks

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,711 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,458 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans