0

this script, trying to get a API token but not sure if this is correct, Google has so many different options, its all over the place and documentation is lacking... I replaced my API values with XXX.

<script runat="server">  
    Platform.Load("Core", "1");  
  
    try {  
        Write("<br>Step 1: Starting Authentication");  
  
        // STEP 1: Authenticate and retrieve the access token  
        var clientId = "XXXX"; // Replace with your actual Client ID  
        var clientSecret = "XXXX"; // Replace with your actual Client Secret  
        var authUrl = "https://XXXXXXXX-vt8.auth.marketingcloudapis.com/v2/token";  
  
        var authPayload = {  
            "client_id": clientId,  
            "client_secret": clientSecret,  
            "grant_type": "client_credentials"  
        };  
  
        Write("<br>Step 1: Sending HTTP POST Request");  
        var authRequest = HTTP.Post(authUrl, "application/json", Stringify(authPayload));  
  
        // Directly parse the response and retrieve the token  
        Write("<br>Step 1: Received Authentication Response"); 
         
        var authResponse = Platform.Function.ParseJSON(authRequest.Response);  
        var accessToken = authResponse.access_token;  
  
        // Log the access token for debugging  
        Write("<br>Access Token Retrieved Successfully: " + accessToken);  
  
    } catch (e) {  
        Write("<br>Exception: " + e.toString());  
    }  
</script> 

the output is:
Step 1: Starting Authentication
Step 1: Sending HTTP POST Request
Step 1: Received Authentication Response
Exception: TypeError: Unable to retrieve security descriptor for this frame.

I know the API is setup right with the right permission as I did a CURL test and it worked, but can't seem to get the Script right, thanks!

3
  • Have you done a search for this error message and/or looked at what is being returned by the call? Either one of these actions would be a good step 2. Commented Aug 14 at 3:14
  • 1
    Think it might be the json parse line. Try the following: var authResponse = Platform.Function.ParseJSON(authRequest.Response[0]); Commented Aug 14 at 12:09
  • thanks August, this worked exactly!!!! Commented Aug 14 at 15:43

1 Answer 1

1

You have to grab the first element in the Response. Here's the auth function that I use.

function getAccessToken(clientId, clientSecret, mid, authURL) {

  var accessToken = "";

  if (clientId != "" && clientSecret != "" && mid != "" && authURL != "") {

      var payload = {
        "grant_type": "client_credentials",
        "client_id": clientId,
        "client_secret": clientSecret,
        "account_id": mid
      };

      var contentType = "application/json";
      var authResult = HTTP.Post(authURL, contentType, Stringify(payload), null, null);
      accessToken = Platform.Function.ParseJSON(authResult["Response"][0]).access_token;

  }

  return accessToken

1
  • thanks! the [0] def worked :) and thanks for the additional function!! you guys are great!!! Commented Aug 14 at 15:43

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.