Discussions

Ask a Question
Back to All

Oauth error when requesting token from Google spreadsheets

I'm trying to pull some data using API v2.0 using Google scripting. When trying to get a token from the server, i'm getting the following error:

Request failed for https://www.humanity.com/oauth2/token.php?client_id=XXXXXXXXXX&client_secret=XXXXXXXXXX&grant_type=password&username=XXXX%40gmail.com&password=XXXX&redirect_uri=http%3A%2F%2Fexample.com returned code 400. Truncated server response: {"error":"invalid_request","error_description":"The grant type was not specified in the request"} (use muteHttpExceptions option to examine full response) (line 41, file "Code")

Even though grant_type=password is present, error says it is not.

Basic code I have is:

function refreshToken() {
  Logger.log("Entering...");
  
  var url = "https://www.humanity.com/oauth2/token.php";
  var client_id = "XXXXXXXXXX";
  var client_secret = "XXXXXXXXXX";
  var grant_type = "password";
  var username = "[email protected]";
  var password = "XXXXXXXXXX";
  var redirect_uri = "http://example.com";
  
  url += "?client_id=" + client_id;
  url += "&client_secret=" + client_secret;
  url += "&grant_type=" + grant_type;
  url += "&username=" + encodeURIComponent(username);
  url += "&password=" + password;
  url += "&redirect_uri=" + encodeURIComponent(redirect_uri);
  
  Logger.log("Url: " + url);
  
  var options = {
    'method' : 'post',
    'contentType' : 'application/x-www-form-urlencoded'
  };
  
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
}

PS: is anyone out there trying to do this google spreadsheet integration as well?