Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

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

  • f23fzf34ffavcz2t56 Profile Picture
    f23fzf34ffavcz2t56 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!

  • Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 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
    f23fzf34ffavcz2t56 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!

  • f23fzf34ffavcz2t56 Profile Picture
    f23fzf34ffavcz2t56 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.

  • Verified answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 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
    f23fzf34ffavcz2t56 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

  • Suggested answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 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

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,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans