D365 Business Central : Test OAuth2 Connection

Last time we talked about how to setup OAuth2 to connect to Business Central API. This time, let’s talk about how to test the connection. We will use two methods: Postman and REST Client.

Postman

We are going to use below URL to test. We are going to put it under URL.

https://api.businesscentral.dynamics.com/v2.0/Sandbox/api/v2.0/companies

Let’s start by setting up the variables first. This will make it easier for us to change the value in the future when needed. Do you remember the three values that we got when we setting up the OAuth2 ? We are going to use it here. Go to Variables and create three variables accordingly: TenantID, ClientSecret, and ClientID. I am using collection variables here.

Time to setup the Authorization Token. Go to Authorization and select Type as OAuth 2.0.

Under Configure New Token, specify Token Name, Access Token URL, Client ID, Client Secret, and Scope.

Access Token URL: https://login.microsoftonline.com/{{TenantID}}/oauth2/v2.0/token
Client ID: {{ClientID}}
Client Secret: {{ClientSecret}}
Scope: https://api.businesscentral.dynamics.com/.default

After that, we can click Get New Access Token to retrieve new token. Postman will not going to automatically retrieve the access token for us.

If everything goes well, we will get Authentication complete. We can then click Use Token to use this token for our API call.

Time to test getting data from Business Central API. Click Send.


Successful result ! If we want to test other tenant, we can just change the variables and do the test.


REST Client

We need the REST client installed in Visual Studio Code to do this. Once installed, we can create a html file with below code (GitHub). We will need to fill in the three variables: tenantId, clientSecret, and clientId.

###OAuth Test with REST Client
@tenantId = c98f6409-fbae-45a4-8317-xxxxxxxxx
@clientId = d4e5a372-b526-45ef-b3c5-xxxxxxxxx
@clientSecret = dAe8Q~Jg04E9fziJZ5tpM5uzxxxxx

@baseUrl = https://api.businesscentral.dynamics.com
@scope = {{baseUrl}}/.default
@bcEnvironmentName = Sandbox
@baseAPIUrl = {{baseUrl}}/v2.0/{{bcEnvironmentName}}/api/v2.0

### Get Authorization Token
# @name auth
POST https://login.microsoftonline.com/{{tenantId}}/oauth2/v2.0/token HTTP/1.1
Content-type: application/x-www-form-urlencoded

grant_type=client_credentials
&client_id={{clientId}}
&client_secret={{clientSecret}}
&scope={{scope}}

### Use Authorization Token
@accessHeader = Bearer {{auth.response.body.$.access_token}}

### Get Companies
# @name GetCompanies
GET {{baseAPIUrl}}/companies HTTP/1.1
Authorization: {{accessHeader}}

If we have REST Client installed, we will see two Send Request actionable links. The first one is to get authorization ticket. The second one is to use the token and get companies data from Business Central API.


Let’s try getting the token first. Click the first actionable link. When all three variables are correct, we will get OK response with the token.


Let’s use the second actionable link to get Business Central company. When the token is correct, we will get OK response with the company data. If we want to test other tenant, we can just change the variables and do the test.

thatnavguy

Experienced NZ-based NAV Developer and Consultant with 15+ years of experience leading multiple IT projects, performing business analyst, developing, implementing, and upgrading Dynamics NAV and Business Central. Passionate to deliver solution that focuses on user-friendly interface while keeping high standard of compliance with the needs.

You may also like...

2 Responses

  1. 5 January 2023

    […] Source : That NAV Guy Read more… […]

  2. 31 October 2023

    […] D365 Business Central : Test OAuth2 Connection – That NAV Guy […]

Leave a Reply

Your email address will not be published. Required fields are marked *