2

I'm trying to obtain an OAuth 2 token through SSJS and getting a 400 error.

Here's the code I'm using:

<script type="javascript" runat="server">    
    Platform.Load("Core", "1.1.5");
    var logDE = "{DE_ExtKey}";
    var log = DataExtension.Init(logDE);
    //get authorization token
    var url = "https://{TenantSpecificEndpoint}.auth.marketingcloudapis.com/V2/Token";
    var contentType = "application/json";
    var payload = '{"grant_type":"client_credentials","clientId":"{ClientIDValue}","clientSecret":"{ClienrSecretValue}"}';
    try {
        var accessTokenResult = HTTP.Post(url, contentType, payload);
        var statusCode = accessTokenResult["StatusCode"];
        var response = accessTokenResult["Response"][0];
        var accessToken = platform.Function.ParseJSON(
            accessTokenResult["Response"][0]
        ).access_token;
        result = Stringify(accessToken).replace(/[\n\r]/g, "");
        log.Rows.Add({ Message: "result: " + result });
    } catch (e) {
        e = Stringify(e).replace(/[\n\r]/g, "");
        log.Rows.Add({ Message: "ErrorMsg: " + e });
    }
</script>

I'm not sure if I'm missing something or if this just isn't supported (yet)? The error message description contains the following: "ExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate a HTTPPost function call." If I swap out the credentials and endpoint for the "classic" OAuth token this works just fine but as the ability to create new credentials using the legacy functionality is going away in a few days it would be good to get this working with OAuth 2.

6

The ClientID and ClientSecret properties for the new API-Version using OAuth2 are named differently, so you need to change "clientId" to "client_id" and "clientSecret" to "client_secret".

Additionally there are some changes which are already correct in your code snippet, but for the sake of completeness: the token in the response is now contained in "access_token", the API route changed and isn't /v1/requestToken anymore, but rather /v2/token and you need to supply a grant_type in your request payload.

Here is a working example code snippet:

var payload = {
    "grant_type": "client_credentials",
    "client_id": "YOUR_CLIENT_ID",
    "client_secret": "YOUR_CLIENT_SECRET"
};
var result = HTTP.Post("YOUR_TENANT_SPECIFIC_AUTH_ENDPOINT/v2/token", 'application/json', Stringify(payload));

if (result.StatusCode == 200) {
    var responseJson = Platform.Function.ParseJSON(result.Response[0]);
    var accessToken = responseJson.access_token;
    var restUrl = responseJson.rest_instance_url;
} else {
    throw new Error("Error fetching access token");
}

Related documentation:

2

It is fully supported; this should work:

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


    var authEndpoint = "your subdomain";
    var client_id = "your clientId";
    var client_secret = "your client_secret";
    var account_id = "your account_id";
    var grant_type = "client_credentials";

    // auth        
    var url = authEndpoint + '/v2/token';
    var contentType = 'application/json';
    var  payload = '{"grant_type":"' + grant_type + '",';
    payload += '"client_id":"' + client_id + '",';
    payload += '"client_secret":"' + client_secret + '",';
    payload += '"account_id":"' + account_id + '"}';

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

    }
    catch (err)  {
      // error
      //Write("error");
    }

</script>

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.