How to add Form Data in PureCloud Action Request

I am trying to integrate PureCloud with a customer provided API via a custom Integration/Action

First step is to request an OAuth token from their authorization API Endpoint.

I can successfully do this using Postman; However in the token request the clientId and ClientSecret are required to be passed as form data and not in header.

e.g.: (In Postman Prerequest Script)

 var client_id = "--TopSecretClientId--";
var client_secret = "--TopSecretClientSecret--)";
var resource = "--YetAnotherTopSecretVariable--";

pm.sendRequest({
    url: "https://TopSecretURL/oauth2/token",
    method: 'POST',
    header: {
        'Content-Type': 'multipart/form-data',
          },
    body: {
        mode: 'formdata',
        formdata: [
            {key: "client_id", value: client_id},
            {key: "client_secret", value: client_secret},
            {key: "resource", value: resource},
            {key: "grant_type", value: "client_credentials"},
        ]
      }
},  function(err, response) {
        const jsonResponse = response.json();
        pm.environment.set("access_token", jsonResponse.access_token);
});

Where I am struggling is how to convert this to my request in my Action Configuration
e.g.:

{
  "requestUrlTemplate": "https://TopSecretURL/oauth2/token",
  "requestType": "POST",
  "headers": {
   	"Content-Type": "multipart/form-data"
  	},
  "body": {
         *** HOW TO SET FORM DATA HERE PLEASE *** 
 }
}

Thanks

Hi Lloyd,
Improving our documentation around form encoding is on our to-do list. Here is an example I have from my notes that is doing the same sort of thing:

{
	"requestTemplate": "grant_type=$esc.url(\"${credentials.grant_type}\")&username=$esc.url(\"${credentials.username}\")&password=$esc.url(\"${credentials.password}\")&client_id=$esc.url(\"${credentials.client_id}\")&client_secret=$esc.url(\"${credentials.client_secret}\")"
}

--Jason

1 Like

Jason,

Cool - Thanks for that. Looks like one step forward, one step sideways for me however.

When I try & test my action, I hit an error at step 8.

8. Execute: The requested resource was not found.
REST call for action execute failed. Message:Request to backend service failed. Response from web service: [f456a24e-2897-41ba-8625-2644db92f8f9]

I can test correctly in Postman, so I know the URL is correct & far side is responding.

So does the ContextId GUID refer to a PureCloud log (or something?) that I can review to see where I'm going wrong still?

 {
  "status": 404,
  "code": "not.found",
  "message": "The requested resource was not found.",
  "messageParams": {},
  "contextId": "f456a24e-2897-41ba-8625-2644db92f8f9",
  "details": [
    {
      "errorCode": "ACTION.REMOTE_ENDPOINT"
    }
  ],
  "errors": [
    {
      "status": 404,
      "code": "NOT_FOUND",
      "message": "REST call for action execute failed. Message:Request to backend service failed.  Response from web service:  [f456a24e-2897-41ba-8625-2644db92f8f9]",
      "messageParams": {},
      "details": [],
      "errors": []
    }
  ]
}

Thanks again

Hi Lloyd,

The contextId is used in our logging to be able to track a transaction, so thanks for including that!

Looking at your data action execution I can see that the foo.bar.com you are trying to hit is accessible via the public internet. I can't see the whole URL though, so please verify verify that the full URL displayed by the "Resolve request URL template" step of test mode works and that the HTTP verb is what you expect.

Another possible solution:

If those suggestions don't resolve your issue please open a case with customer care.
--Jason

Jason,

Thanks for your help, it's got me a bit further, but still not quite there.
Here's where I am currently:

My Request
Note: Currently testing with hard coded Resource/ClientId/ClientSecret

{
"requestUrlTemplate": "https://login.microsoftonline.com/---TopSecretGUIDIsHere---/oauth2/token",
"requestType": "POST",
"headers": {
"Content-Type": "multipart/form-data",
"Transfer-Encoding": "buffered"
},
"requestTemplate": "grant_type=client_credentials&resource={MyResourceId}&client_id={MyClientId}&client_secret={MySecret}"
}

Resolve URL Template

"[POST] https://login.microsoftonline.com/---TopSecretGUIDIsHere---/oauth2/token"

Resolve request header templates

{
"Transfer-Encoding": [
"buffered"
],
"Content-Type": [
"multipart/form-data"
]
}

Resolve request body template
Note: PureCloud put the words redacted in, not me

"grant_type=client_credentials&resource=<REDACTED>&client_id=<REDACTED>&client_secret=<REDACTED>"

Execute

{
"status": 400,
"code": "bad.request",
"message": "The request could not be understood by the server due to malformed syntax.",
"messageParams": {},
"contextId": "702d57f6-0864-4012-aebb-b9342d8e147f",
"details": [
{
"errorCode": "ACTION.REMOTE_ENDPOINT"
}
],
"errors": [
{
"status": 400,
"code": "BAD_REQUEST",
"message": "REST call for action execute failed. Message:Request to backend service failed. Response from web service: {"error":"invalid_request","error_description":"AADSTS900144: The request body must contain the following parameter: 'grant_type'.\r\nTrace ID: 623003ea-d578-4525-ae90-1f72bef77900\r\nCorrelation ID: 8bcd6504-65f6-4e51-96f3-c717ffa55034\r\nTimestamp: 2019-06-18 03:43:21Z","error_codes":[900144],"timestamp":"2019-06-18 03:43:21Z","trace_id":"623003ea-d578-4525-ae90-1f72bef77900","correlation_id":"8bcd6504-65f6-4e51-96f3-c717ffa55034"} [702d57f6-0864-4012-aebb-b9342d8e147f]",
"messageParams": {},
"details": [],
"errors": []
}
]
}

So the response back from Azure is that the 'grant_type' parameter is missing, even though it is definitely there.

My guess is that my parameters aren't in form data, so the Azure side can't retrieve them, hence the error.

I have logged a ticket with Genesys support, but they just sent me some links for how to create an OAuth PureCloud account, and when I pointed out that my issue was connecting to an external system they've gone a bit quiet (about 5 days now)
-Hence back here just in-case someone can help (please)

Should be fixed by adding a header to the config.request for content type of form-urlencoded.
"headers": {
"Content-Type": "application/x-www-form-urlencoded"
}

Legendary! Thank you Greg & Jason. Confirming that this is now working for me.

2 Likes

@Greg_Cunningham: I am also facing the same issue with Salesforce custom data action. Trying to create a case using Salesforce Rest Api
{
"status": 404,
"code": "not.found",
"message": "The requested resource was not found.",
"messageParams": {},
"contextId": "362c6541-338b-4564-a7c1-82ecb4946255",
"details": [
{
"errorCode": "ACTION.REMOTE_ENDPOINT"
}
],
"errors": [
{
"status": 404,
"code": "NOT_FOUND",
"message": "REST call for action execute failed. Message:Request to backend service failed. Response from web service: [{"errorCode":"NOT_FOUND","message":"The requested resource does not exist"}] [362c6541-338b-4564-a7c1-82ecb4946255]",
"messageParams": {},
"details": [],
"errors": []
}
]
}

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.