0

I'm trying to get the Salesforce access token from a connected app in MC and can't get the response. I tried it on Postman through Form Data and could get the token though.

Really appreciate who can take a look and help me find out where my mistakes are being made.

<script runat="server">
Platform.Load("Core", "1.1.1");
// Client information
var client_id = "XXXXXXXXXXX";
var client_sc = "YYYYYYYYYYY";
var username = "myusername"
var password = "mypassword"
var rest_url = "https://myhost.my.salesforce.com/services/oauth2/token";
var Content_type = "application/x-www-form-urlencoded"
var payload = '{"grant_type": "password","client_id": "'+ client_id + '","client_secret": "' + client_sc + '", "username":"'+ username +'", "password": "'+ password +'"   }';

// Get Access token
try {
var res = HTTP.Post(rest_url, Content_type, payload, ['Accept-Encoding'], ['identity']);
    res = Platform.Function.ParseJSON(res.Response[0]);
Write(Stringify(res));
} catch (ex) {
    Write(Stringify(ex));
}
</script>

2 Answers 2

2

I have a slightly different SSJS based solution, which I have been using successfuly in multiple implementations:

     var username = '[email protected]';
     var password = 'myPassword';
     var clientId = 'veryLongClientId';
     var clientSecret = 'verySecretClientSecret';
     var loginurl = "https://login.salesforce.com/services/oauth2/token?grant_type=password&client_id=" + clientId + "&client_secret=" + clientSecret + "&username=" + username + "&password=" + password;

     var gettoken = new Script.Util.HttpRequest(loginurl);
     gettoken.emptyContentHandling = 0;
     gettoken.retries = 2;
     gettoken.continueOnError = true;
     gettoken.contentType = "application/json"
     gettoken.method = "POST"; 
     gettoken.postData = '';
     var tokenrequest = gettoken.send();
     var resultJSON = Platform.Function.ParseJSON(String(tokenrequest.content));
     var token = resultJSON.access_token;
     var auth = "Bearer "+token;
     var url = resultJSON.instance_url;
1
  • Hi @Lucas. Thanks for giving me a new approach. How do we know when the access token will be expired? I can't find the expiry time for that.
    – Duc Le
    Commented Dec 17, 2021 at 0:38
1

I have changed the payload into Form Data and it works. Eventually I can get the response

var payload = "grant_type=password&client_id=" + client_id + "&client_secret=" + client_sc + "&username=" + username + "&password=" + password;

Final script

<script runat="server">
Platform.Load("Core", "1.1.1");
// Client information
var client_id = "XXXXXXXXXXX";
var client_sc = "YYYYYYYYYYY";
var username = "myusername"
var password = "mypassword"
var rest_url = "https://myhost.my.salesforce.com/services/oauth2/token";
var Content_type = "application/x-www-form-urlencoded"
var payload = "grant_type=password&client_id=" + client_id + "&client_secret=" + client_sc + "&username=" + username + "&password=" + password;

// Get Access token
try {
var res = HTTP.Post(rest_url, Content_type, payload);
    res = Platform.Function.ParseJSON(res.Response[0]);
Write(Stringify(res));
} catch (ex) {
    Write(Stringify(ex));
}
</script>

You must log in to answer this question.

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