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 :
Customer experience | Sales, Customer Insights,...
Unanswered

OAuth2 Token 401 Unauthorised

(0) ShareShare
ReportReport
Posted on by 129

I have managed to get a token from oauth2 in my python app. However when I try and get some results I get a 401. Below is my code.

    crmapi = "https://{orgname}.api.crm4.dynamics.com/api/data/v9.1/"
    logging.info('Python HTTP trigger function processed a request.')

    crmorg = 'https://{orgname}.crm4.dynamics.com/'
    clientid = '0e91010b-...….'
    username = username
    userpassword = 'password'
    tokenendpoint = 'login.microsoftonline.com/.../token
    crmwebapi = 'https://{orgname}.api.crm4.dynamics.com/api/data/v9.1'
    crmwebapiquery = '/opportunities(8A49E258-...….)'
    tokenpost = {
    'client_id':clientid,
    'username':username,
    'password':userpassword,
    'grant_type':'password',
    'client_secret''........',
    'scope':'https://{orgname}.crm.dynamics.com//user_impersonation'
    }
    tokenres = requests.post(tokenendpoint, data=tokenpost)
    accesstoken = ''
    try:
        accesstoken = tokenres.json()['access_token']
    except(KeyError):
        #handle any missing key errors
        print('Could not get access token')

    if(accesstoken!=''):
        #prepare the crm request headers
        crmrequestheaders = {
            'Authorization''Bearer ' + accesstoken,
            'OData-MaxVersion''4.0',
            'OData-Version''4.0',
            'Accept''application/json',
            'Content-Type''application/json; charset=utf-8',
            'Prefer''odata.maxpagesize=500',
            'Prefer''odata.include-annotations=OData.Community.Display.V1.FormattedValue'
        }
        crmres = requests.get(crmwebapi+crmwebapiquery, headers=crmrequestheaders)  
    
I have the same question (0)
  • erhan.keskin Profile Picture
    2,253 on at

    Hi,

    Try this one, provide resource.

    nishantrana.me/.../

    Regards,

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans