0

I'm trying to make Auth API calls to an external API Endpoint from Marketing Cloud using SSJS or AMPScript but I am getting 400 bad request errors for these calls. In talks with a contact from the API endpoint, the endpoint is expecting the calls to have a Content-type of "multipart/form-data" whereas most examples online are utilizing 'application/json' type.

I've tried it with AMPScript and SSJS in a Cloudpage, Code Resource, and Email to attempt to debug the code but we continue to get 400 bad request errors.

I attempted to form the payload utilizing the example outlined in this post but the call still resulted in a 400 error.

These Auth calls are successful in postman but we are passing the grant_type and Scope in the "form-data" section of call(s). I tried to pull out the Javascript from Postman and use it in an SSJS code snippet but I didn't have any luck.

Here is my SSJS code with security information redacted.

<script runat="server">
Platform.Load('core', '1.1');
    
var url = '*****REDACTED*****';
      
var boundary = 'Test';
      
var headerNames = ['Customheader', 'Cache-Control'];
    
var headerValues = ['X', 'no-cache'];
    
var contentType = 'multipart/form-data; charset=utf-8; boundary=' + boundary
    
var result = [0];
    
var payload = '';
    payload += '\r\n--' + boundary
    payload += '\r\nContent-Disposition: form-data; grant_type="client_credentials"; scope="****REDACTED****"';
    payload += '\r\n\r\nclient_id,client_secret'
    payload += '\r\n****REDACTED*******,****REDACTED*******'
    payload += '\r\n--' + boundary + '--'
    // Write('Payload: ' + payload)

try
{
    var statusCode = Platform.Function.HTTPPost(url, contentType, payload, headerNames, headerValues, result);
}catch(error) {
    Write(error.description)
}
  
</script>

Here is the error message from an Email preview:

ExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate a HTTPPost function call. See inner exception for details. Error Code: OMM_FUNC_EXEC_ERROR - from Jint --> --- inner exception 1--- System.Net.WebException: The remote server returned an error: (400) Bad Request. - from System

Here is another example where we stringify the payload.

    <script runat="server">
Platform.Load('Core', '1.1')  
    var client_id = '*REDACTED*';
    var client_secret = '*REDACTED*';
    var scope = "*REDACTED*"
      var encode = Base64Encode(client_id + ':' + client_secret);
      var authHeader = 'Basic ' + encode;
  var url = '*REDACTED*';
var contentType = 'multipart/form-data';
  var payload = 'grant_type=client_credentials&scope=*REDACTED*';
  var strpayload = Stringify(payload);
  var headerNames = ["Authorization"];
var headerValues = [authHeader];

 var result = HTTP.Post(url, contentType, strpayload, headerNames, headerValues);
   
</script>

Any help getting these Auth calls up and running would be much appreciated.

3
  • payload += '\r\nContent-Disposition: form-data; grant_type="client_credentials"; scope="****REDACTED****"' is wrong. This needs to be 2 calls: 1) grab access token 2) upload file. The MC -> Pardot post you're referencing does not show the acquisition of the token (they merely reference it as already available), you've deviated from their example.
    – identigral
    Commented Sep 18, 2023 at 3:59
  • Thank you, I'm referencing the past post as they utilized the content-type of "multipart/form-data" in their API Call and I'm having issues with my Auth call accepting this content type. I'm trying to make an Auth call to an external endpoint using that content type unlike the other post where they are trying to make a call from MC to Pardot.
    – Chris G
    Commented Sep 18, 2023 at 13:04
  • n talks with a contact from the API endpoint, the endpoint is expecting the calls to have a Content-type of "multipart/form-data": this is not the full picture/spec. There's an authorization step via oAuth (the client credentials flow). Either your other party is not clearly spelling this out for you or you're not documenting the full spec here in this question. Test these calls via a command-line utility such as curl, then you'll see.
    – identigral
    Commented Sep 18, 2023 at 20:53

0

You must log in to answer this question.

Browse other questions tagged .