I try to Authenticate in salesforce to retrieve some info. I need to do this from SSJS. But when I try to make a POST call to https://login.salesforce.com/services/oauth2/token
I get no content in response. It's empty. I tried the same call with the same body and the same URL in Postman - and it works, I get all the content I need.
Here is my code:
var sfURL = "https://login.salesforce.com/services/oauth2/token";
var data = {
"grant_type":"password",
"client_id":"xxxxxxx",
"client_secret":"xxxxxxxxxxxx",
"username":"xxxxxxxxxx",
"password":"xxxxxxxxxx"
}
var req = new Script.Util.HttpRequest(sfURL);
req.continueOnError = true;
req.method = "POST";
req.setHeader('Content-Type','application/json');
req.postData = Stringify(data);
var resp = req.send();
and after this resp.content
return an empty string. But Postman returns something like this
{
"access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"instance_url": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"token_type": "Bearer",
"issued_at": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"signature": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
So, in other words: it's clearly not empty. And I expected to see the same result in resp.content
. Why content in SSJS is empty?
P.S. i tried to make HTTP call in the same way described here Generating Salesforce OAuth2 token via SSJS returning empty response but then I get error as such "AMP Script is not currently supported in SSJS Activities.". Code:
Platform.Function.TreatAsContent(' %%[Var @response
set @payload = 'grant_type=password' +
'&client_id=XXXXXXXX' +
'&client_secret=XXXXXXXX' +
'&username=XXXXXXXX' +
'&password=XXXXXXXX'
HTTPPost("https://login.salesforce.com/services/oauth2/token", "application/x-www-form-urlencoded", @payload, @response)
/* %%{={{ }}=}%% escape OMM delimiters, do not delete this comment */
]%% ');
var response = Variable.GetValue('@response');
response = Platform.Function.ParseJSON(response);