0

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);
1

1 Answer 1

1

I compared your code with a working example from a recent implementation -

From that I see a couple small things off in your code:

  1. contentType header is set differently.
  2. You needlessly stringify the payload
  3. resp.content needs to be stringified and parsed.

All these things are "formal" errors, which don't apply to POSTMan, so that would be consistent with the behavior you observe.

see markers in code for 1,2,3:


var status = "";
var myURL = "https://login.salesforce.com/services/oauth2/token";
var contentType = "application/json; charset=UTF-8";
var payload = {
            "grant_type":"password",
            "client_id":"xxxxxxx",
            "client_secret":"xxxxxxxxxxxx",
            "username":"xxxxxxxxxx",
            "password":"xxxxxxxxxx"
        }

var req = new Script.Util.HttpRequest(myURL);
req.emptyContentHandling = 0;
req.retries = 0;
req.continueOnError = true;
//1 
req.contentType = contentType;
req.method = "POST";       
//2
req.postData = payload;    

try {
    var response = req.send(); 
//3
    var responseContent = Platform.Function.ParseJSON(String(response.content));
    var statusCode = response.statusCode;
    if (statusCode == 200 || statusCode == 201) {
        status = "success" 
       // you should be able to work with responseContent here.
    } else {
        status = "error " + statusCode

    }
}
catch (err)  
{
    status = "catch " + err
}

I couldn't test this code now but hopefully it puts you on the right track.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .