Q:

Help With Access Token for Server-to-Server Integrations Using SSJS

I am using the script below
    <script runat="server">
        Platform.Load("core","1.1.1");
        var client_id = Variable.GetValue("@ci");
        var client_secret = Variable.GetValue("@cs");
        var authBaseUrl = "https://SUBDOMAIN..auth.marketingcloudapis.com";
        var authEndPoint = "/v2/token";
        var contentType = "application/json";
        var authObj = {};
        authObj.client_id = client_id;
        authObj.client_secret = client_secret;
        authObj.grant_type = "client_credentials";
        authObj.scope = "data_extensions_read data_extensions_write";
        authObj.account_id = "{MID}";  

        try {
            Write("<br>URL: "+authBaseUrl+authEndPoint+"<br>Content Type: " + contentType + "<br>Auth Obj: "+Stringify(authObj)+"<br>");

            var authReq = HTTP.Post(authBaseUrl+authEndPoint, contentType, Stringify(authObj));
            Write(authReq);

            var tokenResp = Platform.Function.ParseJSON(accessTokenResult["Response"][0]);
            Write("<br>Token Response: " + tokenResp);

            var accessToken = tokenResp.access_token;
            Write("<br>Access Token: " + tokenResp);
            if (accessToken !=="") {

                Platform.Response.Write(accessToken);

            }else{
                Platform.Response.Write("Auth failed");
                }
        } catch (e) {
            Write("<br>e: " + Stringify(e));
            Write("<br> Error name: "+Stringify(e.name))
            Write("<br> Error message: "+Stringify(e.message))

        }  

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

        Write("<br><br>type of id: " + typeof(id)+ "<br> ID: "+ id);
        Write("<br>type of pwd: " + typeof(pwd)+ "<br> PWD: "+ pwd);

        Write("<br><br>type of url: " + typeof(authBaseUrl+authEndPoint));
        Write("<br>type of contentType: " + typeof(contentType));
        Write("<br>type of payload: " + typeof(authObj));
        Write("<br>type of payload using function Stringify: " + typeof(Stringify(authObj)));
        Write("<br>url: " + url);
        Write("<br>contentType: " + contentType);
        Write("<br>payload: " + Stringify(authObj));
        </script>

And getting the following error message
"message":"An error occurred when attempting to evaluate a HTTPPost function call. See inner exception for details.",
"description":"ExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate a HTTPPost function call.
See inner exception for details.\r\n Error Code: OMM_FUNC_EXEC_ERROR\r\n - from Jint --> \r\n\r\n --- 
inner exception 1---\r\n\r\nSystem.Net.WebException: 
The remote server returned an error: (400) Bad Request. - from System\r\n\r\n\r\n\r\n"

A:

This should work for OAuth2.0.
account_id is your MID.
client_id, client_secret and authEndpoint are taken from the installed package 
<script type="javascript" runat="server">
Platform.Load("Core","1.1.5");
var account_id = "XXXX";
var grant_type = "client_credentials";
var client_id = "XXXX";
var client_secret = "XXXXX";
var authEndpoint = "https://XXXXX.auth.marketingcloudapis.com/";

// Get API AccessToken
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 + '"}';
var accessToken = "";

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

 }
 catch (err)  {
   // Write("<br>url " + url);
   // Write("<br>contentType: " + contentType);
   // Write("<br>" + payload);
   // Write("<br>error");
}
</script>

