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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Web API returns "401 Unauthorized" even though context is within a web resource and not from external

(0) ShareShare
ReportReport
Posted on by 221

Hello,

This is the first time I'm using the new (to me) Web API. According to the documentation, running it from a web resource doesn't require authentication. Here's my function that I attached to an onChange event of a text field (just for testing purposes).

https://msdn.microsoft.com/en-us/library/mt595798.aspx

When you use the Web API with JavaScript within HTML web resources, form scripts, or ribbon commands you don’t need to include any code for authentication. In each of these cases the user is already authenticated by the application and authentication is managed by the application.

(I used v8.1 because that's what the Developer Resources page says vs what the docs say: v8.2. Anyway both give the same result.)

(Also, this editor is clobbering the URL. /.../ should say /api/data/v8.1/.)

function getDataWebApi() {
    var uri = "xxx.crm.dynamics.com/.../accounts"
    
    var options = [
        "$select=name",
        "$top=3"
    ];

    var fullUri = uri + "?" + options.join("&");

    var request = new Request(fullUri, {
        method: "get"
        , mode: "cors"
        , headers: {
            "Accept": "application/json",
            "OData-MaxVersion": "4.0",
            "OData-Version": "4.0"
        }
    });
    fetch(request).then(function (response) {
        console.log(response);
        return response;
    });
}

Below is what I see in the Chrome console:

Response {type: "basic", url: "xxx.crm.dynamics.com/.../accounts$select=name&$top=3", redirected: false, status: 401, ok: false…}
    body: ReadableStream
    bodyUsed: false
    headers: Headers
    ok: false
    redirected: false
    status: 401
    statusText: "Unauthorized"
    type: "basic"
    url: "xxx.crm.dynamics.com/.../accounts$select=name&$top=3"
    __proto__: Response

Anyone know what I'm doing wrong?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Gopalan Bhuvanesh Profile Picture
    11,401 on at
    RE: Web API returns "401 Unauthorized" even though context is within a web resource and not from external

    Hi

    If you put it in the browser (example: in the address bar of chrome, after logging in to CRM) what do you get?

    xxx.crm.dynamics.com/.../accounts

    Please verify the URL (crm or crm9 ...)

    Verify the url

    Settings -> Customization -> Developer Resources

    Service Root URL in the Instance Web API section

  • f23fzf34ffavcz2t56 Profile Picture
    221 on at
    RE: Web API returns "401 Unauthorized" even though context is within a web resource and not from external

    Hi Gopalan,

    It works when I put it in the address bar. What does that mean?

    - Chris

  • Verified answer
    Gopalan Bhuvanesh Profile Picture
    11,401 on at
    RE: Web API returns "401 Unauthorized" even though context is within a web resource and not from external

    Hi

    I am not sure.

    Do you want to try the following:

    mscrmrocks.wikispaces.com/CRM+Web+API

    scaleablesolutions.com/crud-operations-using-microsoft-dynamic-crm-web-api

  • f23fzf34ffavcz2t56 Profile Picture
    221 on at
    RE: Web API returns "401 Unauthorized" even though context is within a web resource and not from external

    I'll take a look at those links. If I find the answer I'll post it here. Thank you.

  • f23fzf34ffavcz2t56 Profile Picture
    221 on at
    RE: Web API returns "401 Unauthorized" even though context is within a web resource and not from external

    The examples are all using XMLHttpRequest so I switched to that and it's working. I even tried 'no-cors' with fetch() but that still didn't work.

    I guess XMLHttpRequest is what I'll be using going forward. Thank you!

  • Gopalan Bhuvanesh Profile Picture
    11,401 on at
    RE: Web API returns "401 Unauthorized" even though context is within a web resource and not from external

    Hi

    Besides, it looks like, Request is not supported in IE.

    developer.mozilla.org/.../Request

  • f23fzf34ffavcz2t56 Profile Picture
    221 on at
    RE: Web API returns "401 Unauthorized" even though context is within a web resource and not from external

    Thanks for pointing that out!

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#2
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans