Authentication using the REST web interface for MS Dynamics CRM 2016 online

I am trying to access the new REST API to create a server-to-server interface for integrating CRM with other applications, such as a web store, etc.

I tried both ways to get the access token from Azure AD:

Customer credentials

import adal

token_response = adal.acquire_token_with_client_credentials(
    'https://login.microsoftonline.com/abcdefgh-1234-5678-a1b1-morerandomstuff', 
    client_id,
    secret
)

and user / password

import adal

token_response = adal.acquire_token_with_username_password(
    'https://login.microsoftonline.com/abcdefgh-1234-5678-a1b1-morerandomstuff',
    'interface@crm.my_domain.com',
    'my_password'
)

In both cases, token_response gets a token object containing accessToken, refreshToken, expiresIn, etc. Therefore, I do not think that there is a mistake up to this point.

Then I try to make a simple request to the web API:

headers = {'Authorization': '%s %s' % (token_response.get('tokenType'),
                                       token_response.get('accessToken'))}

r = requests.get('https://domain.api.crm4.dynamics.com/api/data/v8.0/Product',
                 headers=headers)

This always returns HTTP 401 - Unauthorized: access denied.

('WWW-Authenticate', 'Bearer error=invalid_token,
error_description=Error during token validation!,
authorization_uri=https://login.windows.net/eabcdefgh-1234-5678-a1b1-morerandomstuff/oauth2/authorize,
resource_id=https://domain.api.crm4.dynamics.com/')

, , Office-365-Administrator, CRM . , - , office-365. Azure AD , " " CRM ( "Access CRM Online " ).

? REST-get-request ? Microsoft API - , - , API ( API ..).

+4
1

acquire_token_with_username_password , :

resource (str, optional): , . 'https://management.core.windows.net/'.

, , CRM, acquire_token_with_username_password:

token_response = adal.acquire_token_with_username_password(
    'https://login.microsoftonline.com/abcdefgh-1234-5678-a1b1-morerandomstuff',
    'interface@crm.my_domain.com',
    'my_password',
    resource='https://domain.crm4.dynamics.com'
)

CRM.

-API ( Product products):

r = requests.get('https://domain.api.crm4.dynamics.com/api/data/v8.0/products',
                 headers=headers)
+3

Source: https://habr.com/ru/post/1622743/


All Articles