Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

HTTP Error 401 - Unauthorized: Access is denied when accessing ms dynamics crm API

Posted on by Microsoft Employee

I am using javascript code to access dynamic CRM API but getting

HTTP Error 401 - Unauthorized: Access is denied

below is my 2 sample codes and both of them giving same error

1.

var createLead1 = function (contant) {
request({
url: 'mydomain.api.crm.dynamics.com/.../accounts',
method: 'POST',
json: {
Name: 'Test Nmae'
}
}, function (error, response, body) {
if (error || response.statusCode != 201) {
console.log(response.body);
console.log('statusCode:' + response.statusCode);

} else {
console.log('success' + response.statusCode)
}

});
}

2.

 var webLead = new Object(); 

webLead.name = 'Test Account';
var jsonwebLead = JSON.stringify(webLead);
var createwebLeadReq = new XMLHttpRequest();

createwebLeadReq.open("POST", "mydomain.api.crm.dynamics.com/.../accounts", true,"username", "password");
createwebLeadReq.setRequestHeader("Accept", "application/json");
createwebLeadReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");

createwebLeadReq.onreadystatechange = function () {

if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
console.log('onreadystatechange1:' + createwebLeadReq.responseText)
}
else {
console.log('onreadystatechange2:' + createwebLeadReq.responseText)

}

};

createwebLeadReq.send(jsonwebLead);
console.log('end');


*This post is locked for comments

  • Suggested answer
    Clem Profile Picture
    Clem 2,541 on at
    RE: HTTP Error 401 - Unauthorized: Access is denied when accessing ms dynamics crm API

    Are you in the context of the CRM when doing the request ?

    If not check this : community.dynamics.com/.../web-api-authentication-from-javascript to authenticate over the CRM and then you will be able to query it.

    Clément

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: HTTP Error 401 - Unauthorized: Access is denied when accessing ms dynamics crm API

    I have also tried this but getting authentication error.

    Actually i want to add the lead data but not able to success.

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: HTTP Error 401 - Unauthorized: Access is denied when accessing ms dynamics crm API

    Hi,

     you mentioned CRM 2011 in the "tags" to thise post.. Is it 365 or 2011?

     Also, are you making those calls from a CRM web resource or from a different web site?

     If you are making those calls from a CRM web resource, are you able to open exactly the same url in a separate browser tab?

     And, finally, did you try "GET" instead of "POST"?  

    msdn.microsoft.com/.../mt607871.aspx

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: HTTP Error 401 - Unauthorized: Access is denied when accessing ms dynamics crm API

    Hi,

    I checked the same location (Settings->Customize the System -> Developer Resource) and found URL

    mydomain.api.crm.dynamics.com/.../v8.2

  • Suggested answer
    Gee Bee Profile Picture
    Gee Bee 11,397 on at
    RE: HTTP Error 401 - Unauthorized: Access is denied when accessing ms dynamics crm API

    Hi

    Check the url

    mydomain.api.crm.dynamics.com/.../accounts

    it could be

    crm5 instead of crm

    Get it from

    Settings->Customize the System -> Developer Resource

    Instance Web API

    Service root URL

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans