Step By Step process to generate Access Token in Salesforce
Go to setup
Quick Find: App
Click on External Client App Manager
Click New
Enter below details
External Client App Name : DataCloudAPIMyScript
API Name: DataCloudAPIMyScript
Distribution State: Local
OAuth Setting
Callback URL: http://localhost:1717/OauthRedirect
OAuth Scopes: You can select all for safer side
Flow Enablement
Select Enable Client Credentials flow
Security
Select Required scerect for Web Server Flow
Require secret for Refresh Token flow
Policies
Permitted Users: All users can self-authorize
Oauth Flows and External Client App Enhancement
Enable Client Credentials flows
Run as Give <username>
Refresh Token Policy
Expire refresh token after specific time
Postman Curl
curl --request POST \
--url "https://<your-mydomain>.my.salesforce.com/services/oauth2/token" \
--header "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "grant_type=client_credentials" \
--data-urlencode "client_id=<your_consumer_key>" \
--data-urlencode "client_secret=<your_consumer_secret>"
SSJS Code
<script runat="server">
Platform.Load("Core", "1.1.1");
try {
// 🔹 Config (replace with your actual values)
var tokenUrl = "https://<your-mydomain>.my.salesforce.com/services/oauth2/token";
var clientId = "<your_consumer_key>";
var clientSecret = "<your_consumer_secret>";
// 🔹 Prepare payload (x-www-form-urlencoded)
var payload = "grant_type=client_credentials" +
"&client_id=" + clientId +
"&client_secret=" + clientSecret;
// 🔹 HTTP Request
var req = new Script.Util.HttpRequest(tokenUrl);
req.emptyContentHandling = 0;
req.retries = 2;
req.continueOnError = true;
req.method = "POST";
req.contentType = "application/x-www-form-urlencoded";
req.postData = payload;
// 🔹 Send request
var response = req.send();
// 🔹 Response Handling
var statusCode = response.statusCode;
var responseBody = response.content;
// 🔹 Logging (you can store this in DE as well)
Write("<br><b>Status Code:</b> " + statusCode);
Write("<br><b>Response:</b> " + String(responseBody));
// Optional: Parse JSON response
if (statusCode == 200) {
var jsonResponse = Platform.Function.ParseJSON(String(responseBody));
var accessToken = jsonResponse.access_token;
Write("<br><b>Access Token:</b> " + accessToken);
} else {
Write("<br><b>Error Occurred while fetching token</b>");
}
} catch (e) {
// 🔹 Error Logging
Write("<br><b>Exception:</b> " + String(e));
}
</script>