web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

401 Unauthorized

(0) ShareShare
ReportReport
Posted on by

i am trying to use web api to update record, but i keep getting this error.

Failed to load resource: the server responded with a status of 401 (Unauthorized)

02218.1.png

*This post is locked for comments

I have the same question (0)
  • ThomasN Profile Picture
    3,190 on at

    Hi ganesh, thanks for reaching out.

    Can you place the code you are using for authentication to CRM?

  • Suggested answer
    ganeshm Profile Picture
    on at

    hi thomasN ,thanks for your reply .
    i am using batch process to make a bulk update for my entities.

    i can get the retrieve value on "results" .but cant update it using web api.

    code:

    var odata = "$select=ContactId,new_age,new_dob&$filter=new_dob ne null";

    SDK.REST.retrieveMultipleRecords("Contact", odata, function(results) {

    var batchId = "1006";
    var changeSetId = "2006";
    var contactList;
    var today = new Date().getFullYear();

    var age = new Date().getFullYear();
    payload = ["--batch_" + batchId]
    payload.push("Content-Type: multipart/mixed;boundary=changeset_" + changeSetId);
    payload.push("");
    for (var i in results) {
    var res = results[i].new_dob;
    var newage = results[i].new_age;
    var contactid = results[i].ContactId;
    var birthyear = res.getFullYear();
    if (birthyear != null) {
    age = today - birthyear;

    payload.push("--changeset_" + changeSetId);
    payload.push("Content-Type: application/http");
    payload.push("Content-Transfer-Encoding:binary");
    payload.push("Content-ID: 1");
    payload.push("");

    payload.push("PATCH " + "365new2016.api.crm8.dynamics.com/.../contacts(" + contactid + ") HTTP/1.1"); //re[i].id
    payload.push("Content-Type: application/json;type=entry");
    payload.push("");
    payload.push(JSON.stringify(age));}

    }
    payload.push("--changeset_" + changeSetId + "--");
    payload.push("--batch_" + batchId + "--");
    var r = payload.join("\r\n");
    var req = new XMLHttpRequest();
    req.open("POST", "365new2016.api.crm8.dynamics.com/.../v8.2" + "$batch");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "multipart/mixed;boundary=batch_" + batchId);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    //req.setRequestHeader("Authorization", "Basic " + ('Z2FuZXNoQDM2NW5ldzMTYub25taWNyb3NvZnQuY29t'+ ":" + 'cGFzc3dvcmQ='))

    req.send(r);

    }, errorHandler, function() {});

    function errorHandler(error) {
    writeMessage(error.message);
    }

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi,

    If we get the above unauthorized access error it could be because either there is problem in the AuthenticationToken, may be we could have assigned wrong organization name or authentication type.

  • ganeshm Profile Picture
    on at

    hello Nithya gopinath,

    well i using  crm online instance.i tried  authenticate it by giving 

    req.setRequestHeader("Authorization", "Basic " + ('Z2FuZXNoQDM2NW5ldzMTYub25taWNyb3NvZnQuY29t'+ ":" + 'cGFzc3dvcmQ='))
    (if u have noticed my code above i have comment the line) 

    can you suggest it by giving some examples using js. 

  • Suggested answer
    ThomasN Profile Picture
    3,190 on at

    ganesh, using CRM online you have to register whatever is making this call with Azure AD as an authorized App with access to your Dynamics Instance.

    Here is a link to how to do this. The token would come from accessing azure.

    msdn.microsoft.com/.../mt622431.aspx

    Let us know how far this gets you.

  • Community Member Profile Picture
    on at

    Hi Ganesh and Tom,

    I'm facing the same issue. But this seemed to be working just fine in Dynamics 365 online trial instance just few weeks back! Tom, could you please explain a little more on what and why I need to register in Azure AD as I'm just hitting the Web API from a javascript web resource, which I suppose will handle all authentication related tokens automatically ( according to this : msdn.microsoft.com/.../mt595798.aspx ).

    Thanks in advance!

  • Suggested answer
    ThomasN Profile Picture
    3,190 on at

    Hi Soundarya,

    Sorry for the misunderstanding. I thought you were going from external application. You are correct no authentication needed at all. No token, etc. The next place to look is at the user security role opening the form and initializing the javascript. They may not have access to web resource records. Or they don't have access to adjust contact records. Check there and let us know what you see.

    Also why do you have authentication code if this is running on web resource?

  • Community Member Profile Picture
    on at

    Hey Tom,

    This is really odd. I'm running as admin and the usual form scripts and SDK calls are working as expected.

    This 401 errors occurs only if I hit the Web API from a web resource, directly querying in the address bar also returns result.

    This is the error message in ErrorLog file "Failed to execute 'send' on 'XMLHttpRequest'" and the network tab says "HTTP Error 401 - Unauthorized: Access is denied".

    What am I missing here?

  • Suggested answer
    ThomasN Profile Picture
    3,190 on at

    Hi Soundarya,

    Let's clarify some things that are being done. Are you use an HTML web resource, or are you creating a javascipt web resource? How are you referencing or calling the javascript that creates the XHR?

    Take a look at this link and see if clears some things up for you. msdn.microsoft.com/.../gg334279.aspx

    Let me know where this gets you.

  • Suggested answer
    ganeshm Profile Picture
    on at

    hey tom,

    thanks for ur help tom.

    i checked the problem , it finally came down with a AuthenticationToken error.(i am facing same batch process error like the link here: stackoverflow.com/.../batch-request-dynamics-crm). 

    as u said before we have to register with azure.you have given a link for it .but can u please help me with some additional link to register with azure.

    before closing this thread can u please help me with few answer.

    i am slightly confused here because i am still calling this js from my crm web resources.

    why do i need azure?does my use of batch request made it necessary?    

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans