Datamapper API Auth2.0 Authorization to get access token


I am tasked with updating documents status in a database using an API with Auth2 authentication.

I am trying to use the createHTTPRequest() function in the datamapper to get an access token to the API before calling the actual URL that updates the status of each record.

I have been unable to get the access token as it seems the createHTTPRequest only supports basic authentication? I am not an expert in REST and I may be mistaken.

Could anyone please provide with a simple sample starter script that one can use to get an access token using Auth2 authentication?

This works fine with the below configuration in Postman, we just don’t know how to represent it in a datamapper or workflow code with an http client

Token Name : Authorization
Grant Type : Password Credentials
Client ID : 12345
Client Secret : testsecret
Username: domain\olconnect
Password : N/A
Scope : api//12345/.default
Client Authentication: Send client credentials in body

Any help will be greatly appreciated and apologies if my request seems trivial, but like I said I am no REST API expert doing this for the first time.

Many thanks.

Hi @robyger,

A working POC in Postman is an excellent starting point. After clicking Get New Access Token you can look at the Console in Postman to see the structures of the request and response. In this case the request consists of a POST with a content type of “application/x-www-form-urlencoded” and the following body:

grant_type: “password”
username: “domain\olconnect”
password: “*****”
scope: “api://B12345/.default”
client_id: “12345”
client_secret: “testsecret”

Now we just need to recreate this using createHTTPRequest(). I have no experience with this part of the API, but based on the documentation I guess it would be something like this:

var body = "grant_type=" + encodeURIComponent("password");
body += "&username=" + encodeURIComponent("domain\\olconnect");
body += "&password=" + encodeURIComponent("*****");
body += "&scope=" + encodeURIComponent("api://B12345/.default");
body += "&client_id=" + encodeURIComponent("12345");
body += "&client_secret=" + encodeURIComponent("testsecret");

var request = createHTTPRequest().open("POST", "http://sampleurl/token", "", "");
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var response = request.send(body);
1 Like

Your example has pointed me in the right direction and it works perfectly!

Thank you so much.