Notifications
Announcements
No record found.
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)
*This post is locked for comments
Hi ganesh, thanks for reaching out.
Can you place the code you are using for authentication to CRM?
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); }
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.
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.
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.
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!
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?
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?
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.
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.
As AI tools become more common, we’re introducing a Responsible AI Use…
We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
SA-08121319-0 4
Calum MacFarlane 4
Alex Fun Wei Jie 2