1

I need to update a contact in Service Cloud from a script of Marketing Cloud. I managed to do it with postman but when i write it in my script, the response is KO.

Here is my code for obtaining the auth token

function getServiceCloudToken() {
 var payload;
 payload = 'client_id:myClientId'
        +  '&client_secret:mySecretId'
        +  '&grand_type:password'
        +  '&username:mySFUserName'
        +  '&password:myPassword'

var url = 'https://test.salesforce.com/services/oauth2/token';
var req = new Script.Util.HttpRequest(url);
req.emptyContentHandling = 0;
req.retries = 2;
req.continueOnError = true;
req.contentType = "multipart/form-data";
req.method = "POST"; 
req.postData = payload;

try{
    var resp = req.send();
}catch(e){
    logDebugRecord("Erreur", Stringify(e));
}}

The return code is '400' with the message 'You have sent us an Illegal URL or an improperly formatted request. '

3
  • The error is saying that it is expecting an object for parse which does not appear in the snippet you provided so I cannot help you figure out what the issue is there. You also do not list a return in your function, so it would come back null/undefined when called. Please share more of the code to provide necessary context. Feb 25, 2021 at 16:34
  • I have edited the question. The previous error message was due to a test of value and was irrelevant.
    – Nehya
    Feb 25, 2021 at 16:43
  • A little off-topic but why don't you simply use the ampscript function if your SFDC account is connected to SFMC: developer.salesforce.com/docs/…
    – EazyE
    Feb 26, 2021 at 13:17

2 Answers 2

1

So the way you are setting up the payload in the call is off. I believe to make your current setup work, you just need to adjust the content type to "application/x-www-form-urlencoded" like so:

req.contentType = "application/x-www-form-urlencoded";

But honestly, I would adjust it to the below to fit better in the SSJS model (utilizing JSON instead of form data):

function getServiceCloudToken() {
 var payload;
 payload = {
    "grand_type": "password",
    "client_id": myClientId,
    "client_secret": mySecretId,
    "username": mySFUserName,
        "password": myPassword
}

var url = 'https://test.salesforce.com/services/oauth2/token';
var req = new Script.Util.HttpRequest(url);
req.emptyContentHandling = 0;
req.retries = 2;
req.continueOnError = true;
req.contentType = "application/json";
req.method = "POST"; 
req.postData = Stringify(payload);

try{
    var resp = req.send();
    return Platform.Function.ParseJSON(String(resp.content));
}catch(e){
    logDebugRecord("Erreur", Stringify(e));
}}

Which first changes the payload into a JSON (which is easier for SSJS to handle and pass) and changed the content-type to 'application/json' to account for the new form of the payload.

The reason for this change above is that the way you were sending the data across in the "multipart/form-data" was incorrect and was the cause of your error. For simplicity's sake, I am recommending JSON, but if you prefer to send across in another format, do so by all means - just validate that the payload is correctly set up or it will keep tossing the same error you get in your question.

1
  • Hi Gortonington, When i use "application/json" i obtain this error : "error": "unsupported_grant_type", I first tested on Postman and it worked only with form-data. I have searched on many websites but i didn't managed to find one giving me the right way to fill my payload.
    – Nehya
    Mar 1, 2021 at 13:15
1

I have found the solution :

var url = 'https://test.salesforce.com/services/oauth2/token'   
        + '?client_id=' + client_id
        + '&client_secret=' + client_secret
        + '&grant_type=' + grant_type
        + '&username=' + username
        + '&password=' + password
        
var req = new Script.Util.HttpRequest(url);
req.retries = 2;
req.continueOnError = true;
req.contentType = "application/x-www-form-urlencoded";
req.method = "POST"; 

try{
    var resp = req.send();
    
    if(resp.statusCode == 200){
        // Récupération du token de connexion           
        var reponse = Platform.Function.ParseJSON(String(resp.content));
        return reponse.access_token;
    }
    
}catch(e){
    logDebugRecord("Erreur", Stringify(e));
} 

I have changed the contentType, stopped using payload and transmitted parameters in url.

You must log in to answer this question.

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