Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

WEB API Authentication: 401 Unauthorized

Posted on by 325

Hi All,

I'm using cURL to test Dynamics WEB API but I'm having 401 error. I was able to successfully post using the WEB API samples provided by Microsoft but our client requires to use cURL. We can get the access token and send a GET message but not POST. I think we need to provide the Dynamics Credential. Can anyone know the exact header to use?

Thanks!

*This post is locked for comments

  • PDCM Profile Picture
    PDCM 325 on at
    RE: WEB API Authentication: 401 Unauthorized

    Hi Michel,

    Thanks for your reply.

    Yeah. That's how I'm doing it and I can make GET work but not POST. Seems it needs the Dynamics Credential to know the privilege in accessing/creating records. Is there something that needs to be done in AD setup to make sure the POST works?

  • Suggested answer
    Michel van den Brink Profile Picture
    Michel van den Brink 4,697 on at
    RE: WEB API Authentication: 401 Unauthorized

    Hello,

    Authenticating with the WebApi works the same way for GET, POST, PATCH and all other verbs.

    Getting a 401 on your request is pretty strange, when that same token does work on a different request. It might be that your token has just expired? Access tokens have a pretty short lifespan (by design)

    Always get your access token first, via Azure AD (login.microsoftonline.com) and make sure it it's valid before you do every request, if not refresh it.

    Send the access token in the 'Authorization' header on every request, GET, POST, or other.

    GET example:

    curl -X GET \
      https://organizationname.crmX.dynamics.com/api/data/v8.2/accounts \
      -H 'authorization: Bearer eyJ0eXAiOiJKV...'


    POST example:

    curl -X POST \
      https://organizationname.crmX.dynamics.com/api/data/v8.2/accounts \
      -H 'authorization: Bearer eyJ0eXAiOiJKV...' \
      -H 'content-type: application/json' \
      -d '{	"name":"New Account Record" }'

    Hope this helps you on your way!

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,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans