Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 46379

Getting 401 when accessing the web API with valid token

$
0
0

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:


 

Any ideas? 


Viewing all articles
Browse latest Browse all 46379

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>