Use Data Factory to get data from Dataverse REST Web API with service principal authentication

I’m trying to pull some data from Dataverse using the Web API with service principal authentication. Everything works fine when I’m doing it in Python script, but encountered some problems when using Copy Data activity in ADF.

Here is my working code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>BASE_URL = 'https://<org-url>.crm.dynamics.com'
credential = DefaultAzureCredential()
token = credential.get_token(BASE_URL + '/.default/)
headers = {
'Authorization': f'Bearer {token}',
'OData-Version': '4.0',
'OData-MaxVersion': '4.0',
'Accept': 'application/json',
}
url = F'{BASE_URL}/api/data/v9.2/<table>'
response = requests.get(url, headers=headers)
</code>
<code>BASE_URL = 'https://<org-url>.crm.dynamics.com' credential = DefaultAzureCredential() token = credential.get_token(BASE_URL + '/.default/) headers = { 'Authorization': f'Bearer {token}', 'OData-Version': '4.0', 'OData-MaxVersion': '4.0', 'Accept': 'application/json', } url = F'{BASE_URL}/api/data/v9.2/<table>' response = requests.get(url, headers=headers) </code>
BASE_URL = 'https://<org-url>.crm.dynamics.com'
credential = DefaultAzureCredential()
token = credential.get_token(BASE_URL + '/.default/)

headers = {
    'Authorization': f'Bearer {token}',
    'OData-Version': '4.0',
    'OData-MaxVersion': '4.0',
    'Accept': 'application/json',
}

url = F'{BASE_URL}/api/data/v9.2/<table>'

response = requests.get(url, headers=headers)

When trying to do something similar in ADF using Copy Data activity, I’m getting one of the errors:

  1. When I put https://<org-url>.crm.dynamics.com in the REST linked service as a Base URL, I get the authentication error 401.
  2. When I put https://<org-url>.crm.dynamics.com/.default in the REST linked service as a Base URL, I pass the authentication, but getting incorrect response, because the endpoint is https://<org-url>.crm.dynamics.com/.default/api/data/v9.2/<table> instead of https://<org-url>.crm.dynamics.com/api/data/v9.2/<table>

How can I solve this? Why is this /.default part needed in order to achieve the token?

0

I created one app registration to call Dataverse API and added it as application user with proper role like this:

To generate access token using service principal authentication, scope value must end with /.default.

For sample purpose, I tried pulling ‘/WhoAmI’ API response by running below python code where client ID, client secret and tenant ID are added as environment variables:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from azure.identity import DefaultAzureCredential
import requests
BASE_URL = 'https://orgxxxxxx.crm.dynamics.com'
credential = DefaultAzureCredential()
token = credential.get_token(BASE_URL + '/.default').token
headers = {
'Authorization': f'Bearer {token}',
'OData-Version': '4.0',
'OData-MaxVersion': '4.0',
'Accept': 'application/json',
}
url = f'{BASE_URL}/api/data/v9.2/WhoAmI'
response = requests.get(url, headers=headers)
print(response.json())
</code>
<code>from azure.identity import DefaultAzureCredential import requests BASE_URL = 'https://orgxxxxxx.crm.dynamics.com' credential = DefaultAzureCredential() token = credential.get_token(BASE_URL + '/.default').token headers = { 'Authorization': f'Bearer {token}', 'OData-Version': '4.0', 'OData-MaxVersion': '4.0', 'Accept': 'application/json', } url = f'{BASE_URL}/api/data/v9.2/WhoAmI' response = requests.get(url, headers=headers) print(response.json()) </code>
from azure.identity import DefaultAzureCredential
import requests

BASE_URL = 'https://orgxxxxxx.crm.dynamics.com'
credential = DefaultAzureCredential()

token = credential.get_token(BASE_URL + '/.default').token

headers = {
    'Authorization': f'Bearer {token}',
    'OData-Version': '4.0',
    'OData-MaxVersion': '4.0',
    'Accept': 'application/json',
}

url = f'{BASE_URL}/api/data/v9.2/WhoAmI'
response = requests.get(url, headers=headers)
print(response.json())

Response:

To retrieve same data using Copy Data activity in ADF and send response to Azure Blob Storage, you can follow below steps:

Initially, create one REST linked service by selecting Authentication type as service principal with below configuration:

Base URL: https://orgxxxxxx.crm.dynamics.com/api/data/v9.2/

Authentication type: Service principal

Service principal ID: appID

Service principal key: client_secret

Tenant: tenantID

Microsoft Entra ID resource: https://orgxxxxxx.crm.dynamics.com

Later, create one REST dataset in ADF and select above linked service with “Relative URL” as WhoAmI (<table> in your case):

In my case, I tried copying data to Azure Blob Storage for which below linked service need to be created:

Later, create one JSON dataset selecting Azure Blob Storage linked service with file path as below:

Now, create one pipeline and add copy data activity to it with source as REST dataset and sink as JSON dataset:

Source:

Sink:

Response:

To confirm that, I checked the file from Azure Blob Storage where data retrieved successfully as below:

UPDATE:

Alternatively, you can also make use of OAuth 2.0 Client Credential method in the linked service and generate the token with ‘/.default’ as scope.

Recognized by Microsoft Azure Collective

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật