1

I am trying to execute some SSJS to store my Marketing Cloud API token into a DE but the script does not seem to work when I run once, as its an activity no error messages are returned so I can't tell what the issue is.

Can someone tell me how I can debug? I have pasted my code below:

<script runat="server">
Platform.Load("Core","1.1.1");

var url = 'https://auth.exacttargetapis.com/v1/requestToken';
var contentType = 'application/json';
var payload = '{"clientId": "myclientkey","clientSecret": "myclientsecret"}';
var headerNames = ["name"];
var headerValues = ["application/json"];
var result = HTTP.Post(url, contentType, payload, headerNames, headerValues);

var stry = (result.Response + '');
var obj = Platform.Function.ParseJSON(stry);
var accessToken = obj.accessToken;

var updateDE = DataExtension.Init('apiDE');
var rows = Platform.Function.UpsertDE("apiDE",["ID"],["1"],["token"],accessToken);

</script>

Edit: I have created a CloudPage to see if I can debug this, however it appears to return a 500 internal error page, it appears as soon as I take out the HTTP.Post line the page loads fine.

As no other information is given, any way I could debug this? or what the possible problems could be with the HTTP.Post command?

  • I'd suggest running this in a landing/cloud page and adding some debugging output. – Adam Spriggs Oct 13 '17 at 17:11
  • Just tried this, I seem to be getting a 500 - Internal Server Error, any ideas how I can debug this? – Deployment Failure Oct 16 '17 at 12:47
  • Comment out everything and uncomment one line at a time until you find the culprit. – Adam Spriggs Oct 16 '17 at 16:12
  • Updated the original post containing which line causes the error already – Deployment Failure Oct 16 '17 at 16:13
2

Here's a request token process I pulled from a production Script Activity. I'm guessing the header names and values are the issue in your code.

<script runat="server">

Platform.Load("Core","1.1.1");

var url = 'https://auth.exacttargetapis.com/v1/requestToken';
var contentType = 'application/json';
var payload = '{"clientId":"CLIENT_ID_HERE","clientSecret":"CLIENT_SECRET_HERE"}';

try {

   var accessTokenResult = HTTP.Post(url, contentType, payload);
   var accessToken = Platform.Function.ParseJSON(accessTokenResult["Response"][0]).accessToken;

} catch (e) {

  Write("<br>e: " + Stringify(e));

}

Write("<br>accessToken: " + accessToken);

if (accessToken !== 'undefined' && accessToken != '') 
    // do stuff here 
}

</script
  • Placed this into a new CloudPage and the same error occurs. – Deployment Failure Oct 16 '17 at 17:10
  • Updated my answer with a try/catch. – Adam Spriggs Oct 16 '17 at 17:29
  • Not sure what is wrong, this still returns the 500 error. I have included the whole CloudPage code incase there is something I am doing wrong: pastebin.com/QsNtqNAC – Deployment Failure Oct 17 '17 at 1:10
2

The ParseJSON in @Adam Spriggs answer failed for me for some reason. This code works for me in mid 2018.

Note that the post response is a JSON object, and there is a Response property that is actually an array with a stringified JSON object inside.

<script runat="server">

Platform.Load("Core","1.1.1");

var url = 'https://auth.exacttargetapis.com/v1/requestToken';
var contentType = 'application/json';
var payload = {
        clientId: "id",
        clientSecret: "secret"
    };

try {

   var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload));
   if(accessTokenRequest.StatusCode == 200) {
        var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]);
        var accessToken = tokenResponse.accessToken;
        Write(accessToken);
   }

} catch (ex) {
    Write("Exception Error: " + Stringify(ex));
}

</script>
1

I managed to save it using the following code, note that I have a DE called LogDE with 2 fields, 'title' and 'text' both Text fields. The code is the following:

<script runat="server">
    Platform.Load('core','1');

        var LOG = DataExtension.Init('LogDE');

        var tokenURL = 'https://auth.exacttargetapis.com/v1/requestToken';
          var payload = {
            "clientId": "<myClientId>",
            "clientSecret": "<myClientSecret>"
        };
        var contentType = 'application/json';
        //var headerNames = ["Content-Type"];
        //var headerValues = ["application/json"];
        var resultToken = HTTP.Post(tokenURL, contentType, Stringify(payload), headerNames, headerValues);

        var myJson;
        /*****************************************/
        try{
            myJson = Platform.Function.ParseJSON(resultToken.Response[0]);
            var logRow ={
             "Title" : "Token",
             "Text" : "token: " + myJson.accessToken
            }

            LOG.Rows.Add(logRow); 
        }catch(exx){
            var logRow ={
             "Title" : "ERROR",
             "Text" : Stringify(exx) 
            }

            LOG.Rows.Add(logRow); 
        }
</script>
0

You can always Write(rows); to the page.

I've gotten this working using the following SSJS, (this is assuming you have already added the ID(Text50) of 1 to the DE as well as the External key - not the name - of the DE is apiDE)

var updateDE = DataExtension.Init('apiDE');
var rows = updateDE.Rows.Update({"token":accessToken}, ["ID"], ["1"]);
  • Writing to the DE is not the problem here, when I take out all lines referencing DE, the issue still occurs – Deployment Failure Oct 16 '17 at 17:11
  • I use the old microsites/landing pages within Email Studio to get errors or remove line by line as Cloudpages hides the errors from you. The error message you get from the page in microsites is: Error Message: Unable to retrieve security descriptor for this frame. Line: 16 Char: 0 var rows = Platform.Function.UpsertDE("apiDE",["ID"],["1"],["token"],accessToken) Description: An unexpected error occurred during the execution of the page. – Shon Gee Oct 17 '17 at 2:20
  • And error for your paste bin is: startIndex cannot be larger than length of string. Parameter name: startIndex – Shon Gee Oct 17 '17 at 2:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.