1
votes

Just finished breakfast and already hit a snag. I'm trying to call the salesforce REST api from my google sheets. I've written a working script locally in python, but converting it into JS, something went wrong:

function authenticateSF(){

  var url = 'https://login.salesforce.com/services/oauth2/token';
  var options = {
    grant_type:'password',
    client_id:'XXXXXXXXXXX',
    client_secret:'111111111111',
    username:'[email protected]',
    password:'smee'
  };

  var results = UrlFetchApp.fetch(url, options);
}

Here is the error response:

Request failed for https://login.salesforce.com/services/oauth2/token returned code 400. Truncated server response: {"error_description":"grant type not supported","error":"unsupported_grant_type"} (use muteHttpExceptions option to examine full response) (line 12, file "Code")

Mind you, these exact parameters work fine in my local python script (putting the key values inside quotations).

Here are the relevant docs: Google Script: Connecting to external API's Salesforce: REST API guide

Thank you all!

1

1 Answers

2
votes

Google's UrlFetchApp object automatically defaults to a GET request. To authenticate, you have to explicitly set in the options the method "post":

function authenticateSF(){

  var url = 'https://login.salesforce.com/services/oauth2/token';
  var payload = {
    'grant_type':'password',
    'client_id':'XXXXXXXXXXX',
    'client_secret':'111111111111',
    'username':'[email protected]',
    'password':'smee'
  };

  var options = {
    'method':'post',
    'payload':payload
  };

  var results = UrlFetchApp.fetch(url, options);
}