0

I'm trying to authenticate to an external service in Marketing Cloud to generate a URL to review a product in Trustpilot.

This is my first time trying to write a SSJS script, so I followed the example in this other question but it won't work and I can't manage to troubleshoot it.

These are all the steps in Trustpilot:

1. Request an access token

To obtain a token you need your API Key, API Secret, Trustpilot username and password.

Method: POST

https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken

Headers:

Authorization: Basic [BASE64_ENCODED(API_KEY:API_SECRET)]

Content-Type: application/x-www-form-urlencoded

The KEY AND SECRET needs to be concatenated as follows KEY:SECRET and then base64 encoded

Payload:

Name - Type - Description

grant_type - Required string - Value must be set to password

username - Required string - Your Trustpilot b2b login email

password - Required string - Your Trustpilot b2b login password

Example: https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken

Header:

Authorization: Basic [BASE64_ENCODED(API_KEY:API_SECRET)]

Payload:

grant_type=password&username=email@domain.com&password=Password

The response will contain amongst other fields the access token, refresh token and time of expiration:

{ access_token: "AccessToken", refresh_token: "RefreshToken", expires_in: "359999" }

Starting from the example in the link I put earlier, I wrote this:

<script type="javascript" runat="server">
Platform.Load("core","1.1.5");

var url = "https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken";

// auth in Trustpilot
var contentType = 'application/x-www-form-urlencoded';
var payload = '{"grant_type":"password",';
    payload += '"username":"luiz.xavier@email.eu",';
    payload += '"password":"0_dR:l3JARt"}';
var headerNames = ["Authorization", "Content-Type"];
var headerValues = ["Basic SHJYejRDcE1pdXN3WFdUpsMWF2SjRLSmtBVw==", "application/x-www-form-urlencoded"];

try {
    // create token
    var accessTokenResult = HTTP.Post(url, payload, headerNames, headerValues);
    var statusCode = result["StatusCode"];
    var response = accessTokenResult["Response"][0];
    var access_token = Platform.Function.ParseJSON(response).access_token;

    var logDE = DataExtension.Init("TrustPilot_API_URL");
    logDE.Rows.Add({"Message":"TOKEN " + access_token });

  try {

    result = Stringify(result).replace(/[\n\r]/g, '');
    logDE.Rows.Add({"Message": "result: " + result});

  } catch (e) {

    e = Stringify(e).replace(/[\n\r]/g, '')
    logDE.Rows.Add({"Message": "error: " + e});

  }
      }
      catch (e)  {
        var logDE = DataExtension.Init("TrustPilot_API_URL");
        logDE.Rows.Add({"Message":"Failed to Run", "Payload": payload});

      }


  </script>

But it's failing time over time in Marketing Cloud and it will just show "Error has ocurred".

Any suggestions what I might be missing?

PS: Email, password and Authorization code have been edited just for this post, so that's not the problem (it worked in Postman).

  • I edited the code and now at least the activity didn't fail and wrote to the Data Extension: "Failed to run" – Luiz Fernando Xavier Nov 20 '19 at 12:40
  • try changing the + in your headerValues to a comma , instead. the plus is concating the values together, meaning your authorization is now both of those and there is no value for your Content-Type. Also should your payload be a string or is it expecting an object? As you currently have it, it is being passed as a String. You also are doing 2 posts to the same URL with same payload. Not sure the logic there. – Gortonington Nov 20 '19 at 13:59
  • Hi @Gortonington, thanks! I had changed the + but forgot to update here. I also changed the code to make one call to the URL only but I still didn't get it working. It was like that in a boilerplate I used as reference so I just kept it, but I got your point that it didn't make much sense. As for the payload being a string or object, I'm honestly not sure. The Trustpilot website only shows: "Payload: grant_type=password&username=email@domain.com&password=Password" I've made some changes to the code and updated the question, but it's still not working – Luiz Fernando Xavier Nov 21 '19 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.