function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zachariah RosenbergZachariah Rosenberg 

Google Apps Script/SF REST API Authentication problem

Hello,

I'm trying to write an REST aPI call from my google sheets. I originally wrote the call in python, which worked wonderfully. Porting it to JS on google apps, I'm getting a "grant_type" error:
 
function authenticateSF(){

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

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

Produces:
 
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")

I saw someone else on this board had a similar problem (https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_SEARCH_RESULT&id=906F00000009At3IAE), and he just went the OAuth route. I'd prefer not to, if possible. 

What might the issue be?

Thank you!
 
Zachariah RosenbergZachariah Rosenberg
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':'ITSME@smee.com', 
    'password':'smee' 
  };

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

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